Need help with VB code for excel macro

Status
Not open for further replies.

skbark

New Member
Joined
Sep 24, 2002
Messages
13
Hello,

I am running into a problem with VB code provided by lasw10. However, his email states he is out of the office until Monday.

Can someone help me tinker with the code? It is supposed to take data from a single sheet and split it up by month into another book that has a sheet for each month. What is happening is only 3 months are created in the new book and each month has a line item from the next month at the bottom. If you would like to see the data I am trying to manipulate, I have included a copy of part of the sheet in another post titled "Linking data from 1 sheet to several sheets in another book?"

Here is the code, which was provided by Lasw10 (Modified slightly by me):

Sub SEPARATE_DATA()

Dim n As Integer
Dim inc As Integer

Application.ScreenUpdating = False

mainfile = "Sandy1.xls"

Workbooks.Open ("C:\Luke Work\Mr_Excel\Sandy2.xls")
monthlyfile = ActiveWorkbook.Name
Windows(mainfile).Activate

n = 2
inc = 0

Do Until Cells(n, 11) = ""
'Determine Sheet Name
SNAME = Cells(n, 11)
Do Until Cells(n, 11) <> Cells(n + inc + 1, 11)
inc = inc + 1
Loop
Windows(monthlyfile).Activate
On Error GoTo ErrorHandler
100
Sheets(SNAME).Select
'Enter Column Headings
Cells(1, 1) = "SAS First"
Cells(1, 2) = "SAS Last"
Cells(1, 3) = "Contract Number"
Cells(1, 4) = "Current Billing Freq"
Cells(1, 5) = "Price"
Cells(1, 6) = "Effective Month"
Cells(1, 7) = "Escalation Code"
Cells(1, 8) = "SpecEsc%"
Cells(1, 9) = "Esc. $"
Cells(1, 10) = "Revised Price"
'Select Copy Range in Columns A & B paste to Column A & B in Sandy2.xls
Windows(mainfile).Activate
Range(Cells(n, 1), Cells(n + inc, 2)).Copy
Windows(monthlyfile).Activate
Sheets(SNAME).Select
Cells(2, 1).PasteSpecial xlPasteValues
Cells(2, 1).PasteSpecial xlPasteFormats
'Select Copy Range in Columns F & G paste to Column C & D in Sandy2.xls
Windows(mainfile).Activate
Range(Cells(n, 7), Cells(n + inc, 8)).Copy
Windows(monthlyfile).Activate
Sheets(SNAME).Select
Cells(2, 3).PasteSpecial xlPasteValues
Cells(2, 3).PasteSpecial xlPasteFormats
'Select Copy Range in Columns J to O paste to Column E onwards in Sandy2.xls
Windows(mainfile).Activate
Range(Cells(n, 10), Cells(n + inc, 15)).Copy
Windows(monthlyfile).Activate
Sheets(SNAME).Select
Cells(2, 5).PasteSpecial xlPasteValues
Cells(2, 5).PasteSpecial xlPasteFormats
'Return to Sandy1.xls and proceed to next month batch
Windows(mainfile).Activate
If inc = 0 Then
n = n + 1
Else
n = n + inc
End If
inc = 0
Loop

'Close and Save Sandy2.xls
Windows(monthlyfile).Activate
ActiveWorkbook.Save
ActiveWorkbook.Close

Application.ScreenUpdating = True

Exit Sub

ErrorHandler:

Sheets.Add
ActiveSheet.Name = SNAME
Resume Next

End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Status
Not open for further replies.

Forum statistics

Threads
1,221,711
Messages
6,161,450
Members
451,707
Latest member
PedroMoss2268

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