I'm having trouble making this macro I wrote run with a command button.
The macro is a demo macro I'm writing for a much larger project, and this is basically what it does:
It allows the user to browse for a pre-formatted file. It then reads the file, stores a few values and plugs it into the designated cells in the workbook the macro is running out of.
The macro itself works when I manually run the macro. However, when I run the macro through the command button, it will execute the first half but not the last half. More specifically, it searches for the file, opens it and makes it the active window, but does not plug in the values in the designated cells.
Sub CommandButton1_Click()
Dim varBook As Variant
Dim otherWb As Workbook
varBook = Application.GetOpenFilename
If varBook <> False Then
Workbooks.OpenText Filename:=varBook
Set otherWb = ActiveWorkbook
'reads & stores cell value
Set r1Cell = Cells(1, 1)
Set r2Cell = Cells(1, 2)
Set r3Cell = Cells(1, 3)
Set inputVariable = Cells(2, 2)
'inserts values into designated cell
ThisWorkbook.Worksheets(2).Cells(1, 1).Value = r1Cell.Value
ThisWorkbook.Worksheets(2).Cells(1, 2).Value = r2Cell.Value
ThisWorkbook.Worksheets(2).Cells(1, 3).Value = r3Cell.Value
ThisWorkbook.Worksheets(2).Cells(6, 2).Value = inputVariable.Value
End If
End Sub
Am I not using the correct format for the command button? or is it something else?
Thanks,
Bookmarks