Userform not adding data to destination file

zack8576

Active Member
Joined
Dec 27, 2021
Messages
271
Office Version
  1. 365
Platform
  1. Windows
I have an excel file that contains a macro that is used to clean up a bunch of csv files.
after I run the macro, I keep the csv file open, then I would use this userform to copy data from the excel that contains the macro to the csv
1676513883887.png


when I select 1250, qty of 1, these data are copied to the csv after the last active row, if qty is 2, then these are copied to the csv twice
1676513979597.png



code below, when I click process, nothing happened. I must be missing something basic...
VBA Code:
Private Sub Process_Click()
    Dim wb1 As Workbook
    Dim wb2 As Workbook
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim lastRow As Long
    
    Set wb1 = ThisWorkbook
    Set wb2 = Workbooks.Open(FileName)
    Set ws1 = wb1.Sheets("Sheet1")
    Set ws2 = ActiveSheet
    lastRow = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
    
    If Me.GTSize1.Value = "1250 gallon" Then
        ws1.Range("B51:K56").Copy
        ws2.Range("A" & lastRow + 1).PasteSpecial xlPasteAll
    End If
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi zack8576. I think you need to activate the sheet in wb2 before setting it or even better specify the sheet name instead of using active sheet (which may be in wb1). Also you have "1250 gallon" when the actual userform is "1250 Gallon". You could use...
Code:
 If Lcase(Me.GTSize1.Value) = Lcase("1250 gallon") Then
HTH. Dave
 
Upvote 0
Solution
Hi zack8576. I think you need to activate the sheet in wb2 before setting it or even better specify the sheet name instead of using active sheet (which may be in wb1). Also you have "1250 gallon" when the actual userform is "1250 Gallon". You could use...
Code:
 If Lcase(Me.GTSize1.Value) = Lcase("1250 gallon") Then
HTH. Dave
thank you Dave
what you recommended worked, thanks again
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top