opening multiple excel worksheets while exporting from access to excel - vba

I am trying to open multiple excel worksheets for one workbook. I have following code but it generates error on Sheets("sheet3").select line.
I don't know why?
Set xlSheet = xlBook.Worksheets(3)
With xlSheet
.Name = "sheet3"
....
....
....
i = 6
Do While Not rsl.EOF
.Range("A" & i).Value = Nz(rsl!x, "")
.Range("B" & i).Value = Nz(rsl!y, "")
.Range("C" & i).Value = Nz(rsl!z, "")
.Range("D" & i).Value = Nz(rsl!xx, "")
.Range("E" & i).Value = Nz(rsl!yy, "")
.Range("F" & i).Value = Nz(rsl!zz, "")
.Range("G" & i).Value = Nz(rsl!xxx, "")
.Range("H" & i).Value = Nz(rsl!yyy, "")
.Range("I" & i).Value = Nz(rsl!zzz, "")
i = i + 1
rsl.MoveNext
Loop
End With
With xlSheet
Set rng = .Range("A6:I5000")
Sheets("sheet3").Select
rng.FormatConditions.Add Type:=xlExpression, Formula1:=""
rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
With rng.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = ANy Color
End With
Set rng = .Range("A6:I5000")
Sheets("sheet3").Select
rng.FormatConditions.Add Type:=xlExpression, Formula1:=""
rng.FormatConditions(rng.FormatConditions.Count).SetFirstPriority
With rng.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = Any color
.TintAndShade = 0
End With
End With
Do I need to replace Sheets("").Select with something else? Or how do I achieve it?
Thanks,

Related

how to lock in between rows in excel vba

I want to lock the in between row's of excel sheet depending the value of the two column's ,
I have following code with me but it's makes entire sheet protected.
the code is :
there is another problem when the loop goes to else part it throws "unable to set Locked property of the range class" the code is :
Do While xlsht.Cells(i, 1) <> vbNullString
If (CStr(xlsht.Cells(i, 54).Value) <> "" And (CStr(Format(xlsht.Cells(i, 55).Value, "dd-MMM-yyyy")) = CStr(Format(Now, "dd-MMM-yyyy")))) Then
.Cells.Locked = False
.Range("A" & i & " : " & "BH" & i).Cells.Locked = True
.Range("A" & i & " : " & "BH" & i).Interior.Color = RGB(255, 255, 0)
.Protect Password:=admin
Else
.Cells.Locked = False
.Range("A" & i & " : " & "AC" & i).Cells.Locked = True
.Range("AE" & i & " : " & "AT" & i).Cells.Locked = True
.Range("BB" & i & " : " & "BH" & i).Cells.Locked = True
.Protect Password:=admin
End If
i = i + 1
Loop
End With
you may be after something like this:
Dim i As Long
i = 1
With Worksheets("mySheetName") '<--| change "mySheetName" to your actual sheet name
Do While .Cells(i, 1) <> ""
If (.Cells(i, 54).Value = "abc" And .Cells(i, 55).Value = "def") Then Intersect(.Range("A:BH"), .Rows(i)).Locked = True
i = i + 1
Loop
.Protect Password:="admin"
End With
By default, the entire sheet is Locked (property of a Range or Cell).
And you can only Protect an ENTIRE sheet.
So you'll have to unLock the rest of the sheet first!
i = 1
With xlsht
.Unprotect Password:=admin
.Cells.Locked = False
Do While xlsht(i, 1) <> vbNullString
If .Cells(i, 54).Values = "abc" And .Cells(i, 55).Values = "def" Then
'here is checking the column depends the row is get lock or not
.Range("A" & i & ":BH" & i).Cells.Locked = True
i = i + 1
End If
Loop
.Protect Password:=admin
End With 'xlsht
Second question
i = 1
With xlsht
.Unprotect Password:=admin
.Cells.Locked = False
Do While .Cells(i, 1).Value <> vbNullString
If CStr(.Cells(i, 54).Value) <> vbNullString And CDate(.Cells(i, 55).Value) = Date Then
With .Range("A" & i & ":BH" & i)
.Cells.Locked = True
.Interior.Color = RGB(255, 255, 0)
End With '.Range("A" & i & ":BH" & i)
Else
.Range("A" & i & ":AC" & i).Cells.Locked = True
.Range("AE" & i & ":AT" & i).Cells.Locked = True
.Range("BB" & i & ":BH" & i).Cells.Locked = True
End If
i = i + 1
Loop
.Protect Password:=admin
End With 'xlsht

vba conditional formatting with standard deviations - run time error with incorrect StDev

I am trying to move some of my conditional formatting to VBA but apply it to ranges that may change every time I run the macro. I think I have defined my ranges, variables, formulas and formats correctly (never done vba format.conditions so syntax could be wrong there). When I run the code it stops on the line
With checkrange.FormatConditions _
.Add(xlCellValue, xlGreater, Formula1:="=R" & cfcll.Row & "C" & q & "+ " & devone & ")")
with run-time error 5, invalid procedure or call.
The full section of code is as follows:
Dim cflastrow As Long
Dim cfrange As Range
Dim cfcll As Range
Dim checkrange As Range
Dim q As Long
Dim devone As Long
Dim devtwo As Long
Dim devthree As Long
Dim devfour As Long
cflastrow = finwb.Sheets("strt_Dash_Final").Cells(Rows.Count, 52).End(xlUp).Row
Set cfrange = finwb.Sheets("Strt_Dash_Final").Range("AV6:AV" & cflastrow)
For Each cfcll In cfrange
If cfcll.value <> "" Then
For q = 4 To 38
Set checkrange = finwb.Sheets("Strt_Dash_Final").Range(Cells((cfcll.Row + 1), q), Cells((cfcll.Row + (cfcll.value - 2)), q))
devone = Application.WorksheetFunction.StDev_P(checkrange)
With checkrange.FormatConditions _
.Add(xlCellValue, xlGreater, Formula1:="=R" & cfcll.Row & "C" & q & "+ " & devone & ")")
With .Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 255
End With
End With
With checkrange.FormatConditions _
.Add(xlCellValue, xlGreater, "=" & Cells(cfcll.Row, q).value & "+ 2*stddev(" & checkrange & ")")
With .Font
.Color = 255
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 49407
End With
End With
With checkrange.FormatConditions _
.Add(xlCellValue, xlGreater, "=" & Cells(cfcll.Row, q).value & "- stddev(" & checkrange & ")")
With .Font
.ThemeColor = xlThemeColorAccent3
.TintAndShade = -0.499984741
.ThemeFont = xlThemeFontMinor
End With
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent3
End With
End With
With checkrange.FormatConditions _
.Add(xlCellValue, xlGreater, "=" & Cells(cfcll.Row, q).value & "- 2*stddev(" & checkrange & ")")
With .Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
.ThemeFont = xlThemeFontMinor
End With
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 5287936
End With
End With
Next q
ElseIf cfcll.value = "" Then
'nada
End If
Next cfcll
Also, despite the range 'checkrange' definitely being correct (checked buy using checkrange = 5 and all values went to 5) devone always comes out as '1' when it should be something like 1.23.....
My theory is that I may not be using R1C1 correctly in the formula context but I saw it used this way in a couple of other examples so am now really not sure. Any help, as always, is greatly appreciated!
In
With checkrange.FormatConditions _
.Add(xlCellValue, xlGreater, Formula1:="=R" & cfcll.Row & "C" & q & "+ " & devone & ")")
the formula will be =R5C4+ 1) if cfcll.Row is 5 and q is 4 and devone is 1.
As you see there is a closing parenthesis to much.
With checkrange.FormatConditions _
.Add(xlCellValue, xlGreater, Formula1:="=R" & cfcll.Row & "C" & q & "+ " & devone)
How to debug? Put the formula in a string variable first
sFormula = "=R" & cfcll.Row & "C" & q & "+ " & devone & ")"
then you would have seen this.
For users having non English Excel versions:
The formulas set for FormatConditions with VBA must be in the language of the Excel. They must not be in US-English as usual in VBA. So R1C1 will be Z1S1 in German Excel for example. Thats weird and annoying.
And to your devone: It is Dimed as Long which is a integer type. So not a wonder that it does not contains Double values.

VBA conditional formatting not applying

I've got a routine that loops through all columns on all worksheets in a workbook to apply a conditional formatting rule to all of them. The rule is saved on each column fine and the rule I've used works, but the colour doesn't change after the routine is run. I can then go into the worksheet, select conditional formatting, click 'edit rule' > 'ok' > 'ok' and the formatting then updates on the worksheet, without having to change anything about the rule. What am I missing to get the rule to actually make a change to the worksheet?
For Each ws In ThisWorkbook.Worksheets
If ws.Name Like "*Management*" Then Exit Sub
lastRow = ws.UsedRange.Row + ws.UsedRange.Rows.Count - 1
lastCol = ws.UsedRange.Column + ws.UsedRange.Columns.Count - 1
ws.Cells.FormatConditions.Delete
For col = 1 To lastCol
Set rng = ws.Range(ws.Cells(2, col), ws.Cells(lastRow, col))
formulaStr = "=BITWISE_AND(2^INDEX(" & wsSchema.Name & "!$" & schemaLastCol & "$2" & _
":$" & schemaLastCol & "$" & schemaLastRow & ", MATCH(1, (" & wsSchema.Name & "!$A$2:$A$" & _
schemaLastRow & "=""" & ws.Name & """)*(" & wsSchema.Name & "!$B$2:$B$" & schemaLastRow & "=$" & _
XLCol(col) & "$1),0)), INDEX($" & XLCol(lastCol) & "$1:$" & XLCol(lastCol) & "$" & lastRow & ",ROW()))"
With rng
.FormatConditions.Add xlExpression, , formulaStr & " = 0"
With .FormatConditions(1)
.Interior.PatternColorIndex = xlAutomatic
.Interior.Color = RGB(255, 0, 0)
.Interior.TintAndShade = 0
.StopIfTrue = False
.SetFirstPriority
End With
.FormatConditions.Add xlExpression, , formulaStr & " > 0"
With .FormatConditions(2)
.Interior.PatternColorIndex = xlAutomatic
.Interior.Color = RGB(0, 255, 0)
.Interior.TintAndShade = 0
.StopIfTrue = False
.SetFirstPriority
End With
End With
Next
Next
Try ThisWorkbook.RefreshAll or Application.Calculate

Multiple cells selection, not range

I want to loop through every row and do some actions for multiple cells selected, e.g. K3,N3,Q3,T3,W3,Z3 next K4,N4,Q4... etc.
What am I doing wrong?
Sub Colors_test()
For counter = 3 To 110
Range("K" & counter, "N" & counter, "Q" & counter, "T" & _
counter, "W" & counter, "Z" & counter).Select
Selection.FormatConditions.AddColorScale ColorScaleType:=3
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).ColorScaleCriteria(1).Type = _
xlConditionValueLowestValue
With Selection.FormatConditions(1).ColorScaleCriteria(1).FormatColor
.Color = 7039480
.TintAndShade = 0
End With
Selection.FormatConditions(1).ColorScaleCriteria(2).Type = _
xlConditionValuePercentile
Selection.FormatConditions(1).ColorScaleCriteria(2).Value = 50
With Selection.FormatConditions(1).ColorScaleCriteria(2).FormatColor
.Color = 8711167
.TintAndShade = 0
End With
Selection.FormatConditions(1).ColorScaleCriteria(3).Type = _
xlConditionValueHighestValue
With Selection.FormatConditions(1).ColorScaleCriteria(3).FormatColor
.Color = 8109667
.TintAndShade = 0
End With
Next counter
MsgBox "Ok All " & counter
End Sub
There is no need to loop. You can use the below code.
Sub Colors_test()
With Range("K3:K110,N3:N110,Q3:Q110,T3:T110,W3:W110,Z3:Z110")
.Select
// your code here
End With
End Sub
You need to pass in the range as 1 argument. Try this:
Range("K" & counter & ",N" & counter & ",Q" & counter & ",T" & counter & ",W" & counter & ",Z" & counter).Select

How to use Excel VBA to activate and copy row data from multiple worksheets in multiple workbooks into another workbook's worksheet?

I have a series of workbooks, containing a series of worksheets, in which I am needing to consolidate those worksheets into one worksheet (they are all identical columns).
I have the below snippet from my combined() sub that I'm trying to use to access each file, iterate over them, get each worksheet inside, and then copy the contents of each worksheet over to the combined.xlsm file.
My problem is, I'm not quite following how I should activate the workbooks/worksheets with my code. Is my code just not going to work?
CombinedWB = "Combined.xlsm"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FLS = FSO.GetFolder("c:\path\to\files").Files
Row = 1
For Each F In FLS
CurrentWB = F.Name
Windows(CurrentWB).Activate
If CurrentWB <> CombinedWB Then
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Combined").Delete
Application.DisplayAlerts = True
If Row = 1 Then
Windows(CombinedWB).Activate
For Each Cell In ActiveSheet.Range("A3")
Worksheets("Combined").Range("A" & Row).Value = "Name"
Worksheets("Combined").Range("B" & Row).Value = "Player"
Worksheets("Combined").Range("C" & Row).Value = Cell.Value
Worksheets("Combined").Range("D" & Row).Value = Cell.Offset(0, 1).Value
Worksheets("Combined").Range("E" & Row).Value = Cell.Offset(0, 2).Value
Worksheets("Combined").Range("F" & Row).Value = Cell.Offset(0, 3).Value
Worksheets("Combined").Range("G" & Row).Value = Cell.Offset(0, 4).Value
Worksheets("Combined").Range("H" & Row).Value = Cell.Offset(0, 5).Value
Worksheets("Combined").Range("I" & Row).Value = Cell.Offset(0, 6).Value
Worksheets("Combined").Range("J" & Row).Value = Cell.Offset(0, 7).Value
Worksheets("Combined").Range("K" & Row).Value = Cell.Offset(0, 8).Value
Worksheets("Combined").Range("L" & Row).Value = Cell.Offset(0, 9).Value
Worksheets("Combined").Range("M" & Row).Value = Cell.Offset(0, 10).Value
Worksheets("Combined").Range("N" & Row).Value = Cell.Offset(0, 11).Value
Worksheets("Combined").Range("O" & Row).Value = Cell.Offset(0, 12).Value
Worksheets("Combined").Range("P" & Row).Value = Cell.Offset(0, 13).Value
Next
Windows(CurrentWB).Activate
Row = 2
End If
For J = 1 To Sheets.Count
Player = Sheets(J).Cells(1).Parent.Name
Injury = Sheets(J).Range("A5").Value
InjuryDate = Sheets(J).Range("B5").Value
For Each Cell In Sheets(J).Range("A5:A100")
Windows(CombinedWB).Activate
If IsEmpty(Cell.Offset(0, 2).Value) <> True Then
Worksheets("Combined").Range("A" & Row).Value = Name
Worksheets("Combined").Range("B" & Row).Value = Player
Worksheets("Combined").Range("C" & Row).Value = Injury
Worksheets("Combined").Range("D" & Row).Value = InjuryDate
Worksheets("Combined").Range("E" & Row).Value = Cell.Offset(0, 2).Value
Worksheets("Combined").Range("F" & Row).Value = Cell.Offset(0, 3).Value
Worksheets("Combined").Range("G" & Row).Value = Cell.Offset(0, 4).Value
Worksheets("Combined").Range("H" & Row).Value = Cell.Offset(0, 5).Value
Worksheets("Combined").Range("I" & Row).Value = Cell.Offset(0, 6).Value
Worksheets("Combined").Range("J" & Row).Value = Cell.Offset(0, 7).Value
Worksheets("Combined").Range("K" & Row).Value = Cell.Offset(0, 8).Value
Worksheets("Combined").Range("L" & Row).Value = Cell.Offset(0, 9).Value
Worksheets("Combined").Range("M" & Row).Value = Cell.Offset(0, 10).Value
Worksheets("Combined").Range("N" & Row).Value = Cell.Offset(0, 11).Value
Worksheets("Combined").Range("O" & Row).Value = Cell.Offset(0, 12).Value
Worksheets("Combined").Range("P" & Row).Value = Cell.Offset(0, 13).Value
Row = Row + 1
End If
Next
Next
End If
Next
EDIT
Here is the final working code (thanks to mwolfe02):
Sub Combine()
Dim J As Integer
Dim Sport As String
Dim Player As String
Dim Injury As String
Dim InjuryDate As String
Dim Row As Integer
Dim FSO As Object
Dim FLS As Object
Dim CurrentWB As String
Dim CombinedWB As String
Dim CombinedWBTemp As String
Dim wb As Workbook
Dim cwb As Workbook
Dim ws As Worksheet
Dim cws As Worksheet
CombinedWB = "Combined.xlsm"
CombinedWBTemp = "~$" & CombinedWB
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FLS = FSO.GetFolder("c:\path\to\files").Files
Set cwb = Workbooks(CombinedWB)
Set cws = cwb.Worksheets("Combined")
cws.Range("A1:Z3200").Clear
Row = 1
For Each F In FLS
CurrentWB = F.Name
If CurrentWB <> CombinedWB And CurrentWB <> CombinedWBTemp Then
On Error Resume Next
Set wb = Workbooks.Open(CurrentWB)
On Error Resume Next
If Not wb.Sheets("Combined") Is Nothing Then
Application.DisplayAlerts = False
wb.Sheets("Combined").Delete
Application.DisplayAlerts = True
End If
If Row = 1 Then
For Each Cell In wb.Sheets(1).Range("A3")
cws.Range("A" & Row).Value = "Sport"
cws.Range("B" & Row).Value = "Player"
cws.Range("C" & Row).Value = Cell.Value
cws.Range("D" & Row).Value = Cell.Offset(0, 1).Value
cws.Range("E" & Row).Value = Cell.Offset(0, 2).Value
cws.Range("F" & Row).Value = Cell.Offset(0, 3).Value
cws.Range("G" & Row).Value = Cell.Offset(0, 4).Value
cws.Range("H" & Row).Value = Cell.Offset(0, 5).Value
cws.Range("I" & Row).Value = Cell.Offset(0, 6).Value
cws.Range("J" & Row).Value = Cell.Offset(0, 7).Value
cws.Range("K" & Row).Value = Cell.Offset(0, 8).Value
cws.Range("L" & Row).Value = Cell.Offset(0, 9).Value
cws.Range("M" & Row).Value = Cell.Offset(0, 10).Value
cws.Range("N" & Row).Value = Cell.Offset(0, 11).Value
cws.Range("O" & Row).Value = Cell.Offset(0, 12).Value
cws.Range("P" & Row).Value = Cell.Offset(0, 13).Value
Next
Row = 2
End If
For Each ws In wb.Worksheets
Player = ws.Cells(1).Parent.Name
Injury = ws.Range("A5").Value
InjuryDate = ws.Range("B5").Value
For Each Cell In ws.Range("A5:A100")
If IsEmpty(Cell.Offset(0, 2).Value) <> True Then
cws.Range("A" & Row).Value = wb.Name
cws.Range("B" & Row).Value = Player
cws.Range("C" & Row).Value = Injury
cws.Range("D" & Row).Value = InjuryDate
cws.Range("E" & Row).Value = Cell.Offset(0, 2).Value
cws.Range("F" & Row).Value = Cell.Offset(0, 3).Value
cws.Range("G" & Row).Value = Cell.Offset(0, 4).Value
cws.Range("H" & Row).Value = Cell.Offset(0, 5).Value
cws.Range("I" & Row).Value = Cell.Offset(0, 6).Value
cws.Range("J" & Row).Value = Cell.Offset(0, 7).Value
cws.Range("K" & Row).Value = Cell.Offset(0, 8).Value
cws.Range("L" & Row).Value = Cell.Offset(0, 9).Value
cws.Range("M" & Row).Value = Cell.Offset(0, 10).Value
cws.Range("N" & Row).Value = Cell.Offset(0, 11).Value
cws.Range("O" & Row).Value = Cell.Offset(0, 12).Value
cws.Range("P" & Row).Value = Cell.Offset(0, 13).Value
Row = Row + 1
End If
Next
Next
wb.Close SaveChanges:=True
End If
Next
Windows(CombinedWB).Activate
Sheets("Combined").Activate
End Sub
Your problems are caused by using the .Activate method. There is no need for that in what you are trying to do. Code created using the macro recorder is littered with .Activate calls, but they are generally a bad idea when writing code yourself.
Try something more like this:
Const CombinedWB As String = "Combined.xlsm"
Dim FSO As Object, FLS As Object, F As Object
Dim wb As Workbook, ws As Worksheet
Dim cwb As Workbook 'This will be our combined workbook'
Dim cws As Worksheet 'This will be the combined worksheet'
Set FSO = CreateObject("Scripting.FileSystemObject")
Set FLS = FSO.GetFolder("c:\path\to\files").Files
Set cwb = Workbooks.Open(CombinedWB)
'Use the following line if there is just a single combined worksheet'
' and it is in the combined workbook'
Set cws = cwb.Worksheets("Combined")
For Each F In FLS
Set wb = Workbooks.Open(F.Name)
If F.Name <> CombinedWB Then
....
'Use the following line if each workbook has a combined worksheet'
Set cws = wb.Worksheets("Combined")
For Each ws In wb.Worksheets
cws.Range("A1") = cws.Range("A1") + ws.Range("A1")
....
Next ws
End If
wb.Close SaveChanges:=True
Next F