If they are that big you might want to read them into 2 massive arrays so that it speeds up - Excel is painfull when you have to go cell by cell
- Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
- Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Excel macro problem
Collapse
X
-
Rule Number 1 - Assuming that you have a valid contract in place always try to get your poo onto your timesheet, provided that the timesheet is valid for your current contract and covers the period of time that you are billing for.
I preferred version 1! -
this is probably wrong as i had no data to test it on..................
Sub fill_data()
Dim vArrayS1 As Variant
Dim vArrayS2 As Variant
Dim vArrayS3 As Variant
Dim S1Counter As Long
Dim S2Counter As Long
Dim lgCounter As Long
Dim lgCounter2 As Long
Dim strUsrid As String
Dim strCatid As String
Dim strCatid2 As String
Dim strFunctid As String
Dim lgOCounter As Long
S1Counter = Sheets(1).UsedRange.Rows.Count - 1
S2Counter = Sheets(2).UsedRange.Rows.Count - 1
ReDim vArrayS1(S1Counter, 1)
ReDim vArrayS2(S2Counter, 1)
lgOCounter = 1
'this could be one function but I couldn't be bothered
For lgCounter = 2 To S1Counter
vArrayS1(lgCounter - 2, 0) = Sheets(1).Cells(lgCounter, 1).Value
vArrayS1(lgCounter - 2, 0) = Sheets(1).Cells(lgCounter, 2).Value
Next lgCounter
For lgCounter = 2 To S2Counter
vArrayS2(lgCounter - 2, 0) = Sheets(2).Cells(lgCounter, 1).Value
vArrayS2(lgCounter - 2, 0) = Sheets(2).Cells(lgCounter, 2).Value
Next lgCounter
For lgCounter = 2 To S1Counter
strUsrid = vArrayS1(lgCounter, 0)
strCatid = vArrayS1(lgCounter, 1)
For lgCounter2 = 2 To S2Counter
strCatid2 = vArrayS2(lgCounter2, 0)
strFunctid = vArrayS2(lgCounter2, 1)
If strCatid = strFunctid Then
Sheets(3).Cells(lgOCounter, 1).Value = usrid
Sheets(3).Cells(lgOCounter, 2).Value = catid
Sheets(3).Cells(lgOCounter, 3).Value = functid
lgOCounter = lgOCounter + 1
End If
Next lgCounter2
Next lgCounter
End SubRule Number 1 - Assuming that you have a valid contract in place always try to get your poo onto your timesheet, provided that the timesheet is valid for your current contract and covers the period of time that you are billing for.
I preferred version 1!Comment
-
Speed isnt so much the problem as much as the fact that the combination of the two sheets will exceed the hard limit on numbers of rows in excel. Hence the multiple sheet idea."Being nice costs nothing and sometimes gets you extra bacon" - Pondlife.Comment
-
OK then....
Sub fill_data()
Dim vArrayS1 As Variant
Dim vArrayS2 As Variant
Dim vArrayS3 As Variant
Dim S1Counter As Long
Dim S2Counter As Long
Dim lgCounter As Long
Dim lgCounter2 As Long
Dim strUsrid As String
Dim strCatid As String
Dim strCatid2 As String
Dim strFunctid As String
Dim lgOCounter As Long
dim intSheetNum as integer
S1Counter = Sheets(1).UsedRange.Rows.Count - 1
S2Counter = Sheets(2).UsedRange.Rows.Count - 1
ReDim vArrayS1(S1Counter, 1)
ReDim vArrayS2(S2Counter, 1)
lgOCounter = 1
'this could be one function but I couldn't be bothered
For lgCounter = 2 To S1Counter
vArrayS1(lgCounter - 2, 0) = Sheets(1).Cells(lgCounter, 1).Value
vArrayS1(lgCounter - 2, 0) = Sheets(1).Cells(lgCounter, 2).Value
Next lgCounter
For lgCounter = 2 To S2Counter
vArrayS2(lgCounter - 2, 0) = Sheets(2).Cells(lgCounter, 1).Value
vArrayS2(lgCounter - 2, 0) = Sheets(2).Cells(lgCounter, 2).Value
Next lgCounter
For lgCounter = 2 To S1Counter
strUsrid = vArrayS1(lgCounter, 0)
strCatid = vArrayS1(lgCounter, 1)
intsheetnum = 3
For lgCounter2 = 2 To S2Counter
strCatid2 = vArrayS2(lgCounter2, 0)
strFunctid = vArrayS2(lgCounter2, 1)
If strCatid = strFunctid Then
Sheets(3).Cells(lgOCounter, 1).Value = usrid
Sheets(3).Cells(lgOCounter, 2).Value = catid
Sheets(3).Cells(lgOCounter, 3).Value = functid
if lgocounter = 65000 then
lgocounter = 1
sheets.add
intsheetnum = intsheetnum+1
else
lgOCounter = lgOCounter + 1
End If
end if
Next lgCounter2
Next lgCounter
End Sub
So when the row count on the output sheet hit 65000 it adds a new sheet ad starts from 1 againRule Number 1 - Assuming that you have a valid contract in place always try to get your poo onto your timesheet, provided that the timesheet is valid for your current contract and covers the period of time that you are billing for.
I preferred version 1!Comment
-
You can pretend Excel is Access (well, sort of).
If you create named ranges on each of your 2 data sources, the Excel ODBC provider will see them as data tables. So if you create a DSN for your spreadsheet, you should then be able to query and join the 2 tables.
You can even return the data back into the same spreadsheet: maybe using the MS Query tool that's built into Excel, or maybe using some ADO code sitting behind a user form or other macro.
I was reading a website a couple of weeks ago that explained this really well, but I'm darned if I can find it now.Comment
-
Aye it works I've done it in the past. Not a bad option if you hate VB but are compentent at SQL.Originally posted by thunderlizard View PostYou can pretend Excel is Access (well, sort of).
If you create named ranges on each of your 2 data sources, the Excel ODBC provider will see them as data tables. So if you create a DSN for your spreadsheet, you should then be able to query and join the 2 tables.Moving to Montana soon, gonna be a dental floss tycoon
Comment
-
-
Got there in the end with some butchery on Pondlifes script and bit's and pieces from everyone else. Thanks for the help everyone. First codeing of any kind i've done in years and first bit of VB ever
"Being nice costs nothing and sometimes gets you extra bacon" - Pondlife.Comment
- Home
- News & Features
- First Timers
- IR35 / S660 / BN66
- Employee Benefit Trusts
- Agency Workers Regulations
- MSC Legislation
- Limited Companies
- Dividends
- Umbrella Company
- VAT / Flat Rate VAT
- Job News & Guides
- Money News & Guides
- Guide to Contracts
- Successful Contracting
- Contracting Overseas
- Contractor Calculators
- MVL
- Contractor Expenses
Advertisers

Comment