VBA to copy/paste

DDT~123

Board Regular
Joined
May 31, 2012
Messages
220
Greetings All,

I have a spreadsheet which sends account information every 30 minutes 24/7 via Outlook to company executives. My original code worked but was a little buggy sometimes and tied up my clipboard until the script was through copy/pasting. I posted in this forum and received a new code which used a different method to copy/paste the data. The only problem with the new code is that it only works if I'm logged in and my computer is unlocked. I'm pasting only a smidgen of the code below as the rest of the code which sends the email works fine. Ideally, I'd like to use the new code but needing it to work regardless of me being at my desk.


New Code
Code:
Public Sub ImportCMS()


Application.ScreenUpdating = False


Dim lastMod As String
Dim ws As Worksheet
Dim File1, File2 As Workbook
File1 = ActiveWorkbook.Name




Set File1 = ActiveWorkbook
Set ws = File1.Sheets("Alpha Widgets")
    ws.Range("A10:R64").ClearContents
lastMod = FileDateTime("C:\Users\Desktop\ServiceLevelsSMS\IntervalScriptOutput\Alpha Widgets.txt")
    ws.Range("B2") = lastMod
Set File2 = Workbooks.Open("C:\Users\Desktop\ServiceLevelsSMS\IntervalScriptOutput\Alpha Widgets.txt")
With File2.Sheets(1).Range("A1:R55")
    ws.Range("A10").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
File2.Close False


Set File1 = ActiveWorkbook
Set ws = File1.Sheets("Bravo Sprockets")
    ws.Range("A10:R64").ClearContents
lastMod = FileDateTime("C:\Users\Desktop\ServiceLevelsSMS\IntervalScriptOutput\Bravo Sprockets.txt")
    ws.Range("B2") = lastMod
Set File2 = Workbooks.Open("C:\Users\Desktop\ServiceLevelsSMS\IntervalScriptOutput\Bravo Sprockets.txt")
With File2.Sheets(1).Range("A1:R55")
    ws.Range("A10").Resize(.Rows.Count, .Columns.Count).Value = .Value
End With
File2.Close False


Original Code
Code:
Public Sub ImportCMS()


Dim File1, File2, File3, File4, File5, File6, File7 As String
File1 = ActiveWorkbook.Name


Sheets("Alpha Widgets").Select
Range("A10:R64").ClearContents
WS = ActiveSheet.Name
File2 = "C:\Users\Desktop\ServiceLevelsSMS\IntervalScriptOutput\Alpha Widgets.txt"
Workbooks.Open File2
wFile = ActiveWorkbook.Name
Range("A1:R55").Select
Selection.Copy
Windows(File1).Activate
Sheets(WS).Activate
Range("A10").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Workbooks(wFile).Close False
If ActiveSheet.Name = "Sheet1" Then
Application.GoTo Reference:="mTop"
End If




Sheets("Bravo Sprockets").Select
Range("A10:R64").ClearContents
WS = ActiveSheet.Name
File3 = "C:\Users\Desktop\ServiceLevelsSMS\IntervalScriptOutput\Bravo Sprockets.txt"
Workbooks.Open File3
wFile = ActiveWorkbook.Name
Range("A1:R55").Select
Selection.Copy
Windows(File1).Activate
Sheets(WS).Activate
Range("A10").Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Workbooks(wFile).Close False
If ActiveSheet.Name = "Sheet1" Then
Application.GoTo Reference:="mTop"
End If
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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