What's wrong with this part of my macro?

Sunvisor

Board Regular
Joined
Oct 9, 2009
Messages
233
Code:
Windows("MRCollect.xls").Activate
  Set ws = ActiveWorkbook.Sheets(1)
  lngLastRow1 = ws.Range("A65536").End(xlUp).Row + 1
  Range("A" & lngLastRow1).PasteSpecial xlPasteValues
  Application.CutCopyMode = False

AT this part of the code I already have MRCollect activated so I dont need that, and also I'd like for it to go to the second workbook which is called "TEMPLATE"....

I am getting the error around the lngLastRow1

Anyone see anything wrong?
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Code:
Windows("MRCollect.xls").Activate
  Set ws = ActiveWorkbook.Sheets(1)
  lngLastRow1 = ws.Range("A65536").End(xlUp).Row + 1
  Range("A" & lngLastRow1).PasteSpecial xlPasteValues
  Application.CutCopyMode = False
AT this part of the code I already have MRCollect activated so I dont need that, and also I'd like for it to go to the second workbook which is called "TEMPLATE"....

I am getting the error around the lngLastRow1

Anyone see anything wrong?

From an amateur point of view, it looks like lngLastRow1 is trying to set a value of 65537....are you in a version of Excel that allows that many rows?
 
Upvote 0
replace it with this
PHP:
ws.cells(rows.count,1).End(xlUp).Row + 1
let me know how it goes
 
Upvote 0
Thanks, I dont think that part is the problem actually...
When I tried to change
Code:
        Set ws = ActiveWorkbook.Sheets(1)
To sheets(2), nothing works like its supposed to

Also I am trying to make something like

Code:
Windows("Meter Read Collection3.xls").Sheets("Sheet2").Activate

to work but its not! =[
 
Upvote 0
oh duh... I can't believe I missed that.

instead of "Windows" use "Workbooks"
 
Last edited:
Upvote 0
Code:
Sub MRcollect_Click()
Dim path As String
Dim FileName As String
Dim Wkb As Workbook
Dim wsmf As Worksheet
Dim lngLastRow1 As Long
Dim wkb1 As Workbook
Dim rng As Range
    
Call ToggleEvents(False)
    '###################################
    path = "C:\Documents and Settings\M08040\My Documents\Test1"  'Change as needed '(Petrina's computer)
    '###################################
    FileName = Dir(path & "\*.xls", vbNormal)
    
    Do Until FileName = ""
        
        'this opens the workbook in the above specified folder
        Set Wkb = Workbooks.Open(FileName:=path & "\" & FileName)
        
        'set this to the sheet number to look at
        Set wsmf = Wkb.Sheets(1)
        
        'moves to next spreadsheet if no "Job Description" is not found
        On Error GoTo NotFound
        LR = Range("B" & Rows.Count).End(xlUp).Row
        Range("B5:N" & LR).Copy
       'copies only if there is a serial number
        
        
        'pastes the information in the last row of your spreadsheet
        Workbooks("MRCollect.xls").Activate
        Set ws = ActiveWorkbook.Worksheets("Template Compiler")
        lngLastRow1 = ws.Range("A35536").End(xlUp).Row + 1
        Range("A" & lngLastRow1).PasteSpecial xlPasteValues
        Application.CutCopyMode = False
    
NotFound:
    FileName = Dir()
    
    Wkb.Close
    
    Loop
Call ToggleEvents(True)
End Sub
Sub ToggleEvents(blnState As Boolean)
    
    With Excel.Application
        .DisplayAlerts = blnState
        .EnableEvents = blnState
        .ScreenUpdating = blnState
        If blnState Then .CutCopyMode = False
        If blnState Then .StatusBar = False
    End With
    
End Sub

Here's the full code that just doesn't seem to work right
 
Upvote 0

Forum statistics

Threads
1,224,590
Messages
6,179,763
Members
452,940
Latest member
rootytrip

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