VBA command button [Solved/Closed]

Posts
6
Registration date
Friday September 21, 2018
Status
Member
Last seen
October 2, 2018
-
Hi Everyone,

I need help creating the following:

I want to create a CommandButton that when clicked it will check column "CB" for "YES" and will input on a different worksheet called "Update Report" what is in columns "A, B, D, AO, BF, BG"

I would really appreciate the help!

Thanks,

Sergio
See more 

3 replies

Best answer
Posts
13028
Registration date
Monday June 3, 2013
Status
Moderator
Last seen
September 20, 2019
1401
1
Thank you
OK, I will help, but you have to do the work!

Here is how to get the value out of a sheet:

Thevariable = ThisWorkbook.Worksheets("SheetName").Range("M3").value

That will populate the "Thevariable" Variable with the value in Sheetname.M3.

Then, to cut the line:

ActiveSheet.Range("A" & whatrow).EntireRow.Select
Selection.Copy


Then to past the entire line, it is:


Private Function copyRow(whatrow, whatsheet)
ActiveSheet.Paste Destination:=Worksheets(whatsheet).Range("A" & whatrow)
Application.CutCopyMode = False
End Function


To deploy the above code:

copyrow(3,"SheetNameOfDestination")


In order to go through each line, you use:

X = Cells(ThisWorkbook.Worksheets(whatsheet).Rows.Count, 1).End(xlDown).Row


Now you loop using X on column CB!

Have fun!

Say "Thank you" 1

A few words of thanks would be greatly appreciated. Add comment

CCM 6719 users have said thank you to us this month

Srojassg
Posts
6
Registration date
Friday September 21, 2018
Status
Member
Last seen
October 2, 2018
-
Thank you for the help! I really appreciate it!
Posts
1248
Registration date
Thursday July 24, 2014
Status
Moderator
Last seen
August 17, 2019
205
1
Thank you
Hello Sergio,

Following is the link to a sample file that I used to help another Poster over a year ago:-

http://ge.tt/347f3nr2

That Poster's query was very similar to yours and may be just what could help you out. Click on the "RUN" button to see it work.

The code in that file is as follows:-

Sub Test()

Application.ScreenUpdating = False

Sheet2.UsedRange.Offset(1).Clear

With Sheet1.[A1].CurrentRegion
        .AutoFilter 16, "Yes"
        Union(.Columns("B:D"), .Columns("H:J"), .Columns("M")).Offset(1).Copy
        Sheet2.Range("A" & Rows.Count).End(3)(2).PasteSpecial xlValues
        .AutoFilter
End With

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub


It transfers only certain columns of data per row from Sheet1 to Sheet2. If you read through the code carefully, you should be able to adapt it to your situation.

Let us know if you need any further help.

I hope that this helps.

Cheerio,
vcoolio.

Say "Thank you" 1

A few words of thanks would be greatly appreciated. Add comment

CCM 6719 users have said thank you to us this month

Srojassg
Posts
6
Registration date
Friday September 21, 2018
Status
Member
Last seen
October 2, 2018
-
Thank you very much for your help. That code worked perfectly and very easy to implement and use!
ac3mark
Posts
13028
Registration date
Monday June 3, 2013
Status
Moderator
Last seen
September 20, 2019
1401 -
I hope you learned something!
vcoolio
Posts
1248
Registration date
Thursday July 24, 2014
Status
Moderator
Last seen
August 17, 2019
205 -
You're welcome Sergio.
I'm glad that we were able to help.

Cheerio,
vcoolio.
Srojassg
Posts
6
Registration date
Friday September 21, 2018
Status
Member
Last seen
October 2, 2018
-
You guys were amazing last time. It helped me tremendously! And I wanted to ask you one more question.

If I wanted to ask you if it was possible to save it into a different Workbook. I have been trying to change the code but I am having a hard time.

Thank you in advance.
Posts
1248
Registration date
Thursday July 24, 2014
Status
Moderator
Last seen
August 17, 2019
205
1
Thank you
Hello Sergio,

I've amended the above code for you which will allow you to transfer the data to a new work book, as follows:-


Sub Test2()

        Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
        Dim wb As Workbook

Application.ScreenUpdating = False

Workbooks.Open Filename:="C:\Users\YOUR FILE PATH HERE\MONSTER.xlsx" '---->Change the file name to suit.
Set wb = Workbooks("MONSTER.xlsx") '---->Change the file name to suit.
wb.Activate

With ws.[A1].CurrentRegion
        .AutoFilter 16, "Yes"
        Union(.Columns("B:D"), .Columns("H:J"), .Columns("M")).Offset(1).Copy
        wb.Worksheets("Sheet1").Range("A" & Rows.Count).End(3)(2).PasteSpecial xlValues
        .AutoFilter
End With
    
Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub


The code assumes that you already have created a new workbook.
You'll have to change the file path and file name to suit yourself.

I hope that this helps.

Cheerio,
vcoolio.

...........and yes, I do have a file named "MONSTER.xlsx" !!

Say "Thank you" 1

A few words of thanks would be greatly appreciated. Add comment

CCM 6719 users have said thank you to us this month

Srojassg
Posts
6
Registration date
Friday September 21, 2018
Status
Member
Last seen
October 2, 2018
-
Thank you very much. It helps a lot! Your code is always easy to follow and implement!

.......and hey, I used to create a used to name my important files funny names so no one would suspect to open it.
vcoolio
Posts
1248
Registration date
Thursday July 24, 2014
Status
Moderator
Last seen
August 17, 2019
205 -
You're welcome Sergio. Glad that I was able to help again.

Cheerio,
vcoolio.
Respond to vcoolio