Excel/vba code

mkostas66

New Member
Joined
Oct 17, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hi!

I have the following code. It supposed to copy certain cells to a new book.
If i run the code in VBA with F5 it's working but when i hit the button with the code in the excel i get 0 copies.

Any ideas what's wrong?

VBA Code:
Sub UpdateActionPlan()

Dim intAdded As Integer, intSRow, intTRow
Dim sngRecord(6)
Dim strTFullPath As String
Dim strText As String
Dim strResponse As VbMsgBoxResult
Dim cancel As Integer
Dim strFileName As String
Dim i As Integer
Dim LastRow As Integer
Dim FirstRow As Integer

FirstRow = 7
LastRow = 1000
i = FirstRow


strFileName = “Test.xls”
strTFullPath = strFileName
Workbooks.Open filename:=strTFullPath

Do Until i > LastRow
If Range("I" & i).Value = "Action" Then
 sngRecord(0) = Range("C" & i).Value
 sngRecord(1) = Range("G" & i).Value
 sngRecord(2) = Range("N" & i).Value
 sngRecord(3) = Range("O" & i).Value
 sngRecord(4) = Range("P" & i).Value
 sngRecord(5) = Range("T6").Value

 With Workbooks("Test.xlsx").Worksheets("CheckSheet")
       intTRow = Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
      .Range("C" & intTRow) = sngRecord(0)
      .Range("E" & intTRow) = sngRecord(1)
      .Range("G" & intTRow) = sngRecord(2)
      .Range("A" & intTRow) = sngRecord(3)
      .Range("D" & intTRow) = sngRecord(4)
     .Range("B" & intTRow) = sngRecord(5)
 End With
Else
 Cancel = True
End If

i = i + 1
Loop

End Sub
 
Ignore data in column R please.

Code:

Option Explicit

Sub Button36_Click()
Dim intTRow As Long
Dim strFileName As String
Dim i As Integer
Dim ValOfT6 As Variant 'The value of cell T6, change to a better name

strFileName = "Test.xlsx"
Workbooks.Open filename:=strFileName
ValOfT6 = Range("T6").Value
For i = 7 To 1000
If Range("S" & i).Value = "Action" Then
With Workbooks("Test.xlsm").Worksheets("Checksheet")
intTRow = .Cells(.Rows.Count, 1).End(xlUp).Offset(1, 0).Row
.Range("C" & intTRow) = Range("C" & i).Value
.Range("E" & intTRow) = Range("G" & i).Value
.Range("G" & intTRow) = Range("N" & i).Value
.Range("A" & intTRow) = Range("O" & i).Value
.Range("D" & intTRow) = Range("P" & i).Value
.Range("B" & intTRow) = ValOfT6
End With
End If
Next i
End Sub
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,215,042
Messages
6,122,810
Members
449,095
Latest member
m_smith_solihull

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