I have the following code that is intended to create a series of combo boxes on a worksheet. Since I cannot be in break mode when creating combo boxes I am struggling to find out what I am doing wrong.
Private Sub CreatePlayerSelectorComboBoxes()
Application.ScreenUpdating = True
Dim currStatusBarMgr As StatusBarManager
Set currStatusBarMgr = New StatusBarManager
currStatusBarMgr.MessagePrefix = "Creating Control: "
With MatchesTeamPlayersWS
.Range(.Cells(17, 1), .Cells(17, .UsedRange.Columns.Count)).ClearContents
Dim matchCounter As Long
For matchCounter = 1 To 6
Dim controlColumnMatch As Long
controlColumnMatch = ((matchCounter - 1) * 24)
Dim matchText As String
matchText = "M" & matchCounter
Dim teamCounter As Long
For teamCounter = 1 To 2
Dim controlColumnTeam As Long
controlColumnTeam = ((teamCounter - 1) * 12)
Dim teamText As String
Select Case teamCounter
Case Is = 1
teamText = "TmA"
Case Is = 2
teamText = "TmB"
End Select
Dim positionCounter As Long
For positionCounter = 1 To 4
Dim positionText As String
positionText = "P" & positionCounter
Dim controlText As String
controlText = matchText & teamText & positionText
Dim currDivAControlName As String
currDivAControlName = "DivA" & controlText
Dim currDivBControlName As String
currDivBControlName = "DivB" & controlText
Dim controlColumnPosition As Long
controlColumnPosition = 3 + ((positionCounter - 1) * 3)
Dim controlColumn As Long
controlColumn = controlColumnMatch + controlColumnTeam + controlColumnPosition
Dim controlCell As Range
Set controlCell = .Cells(17, controlColumn)
currStatusBarMgr.PostStatusBarUpdate (currDivAControlName)
Debug.Print currDivAControlName
Dim controlDivA As Variant
Set controlDivA = .OLEObjects.Add(ClassType:="Forms.ComboBox.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=controlCell.Left, _
Top:=controlCell.Top, _
Width:=140, _
Height:=24)
controlDivA.Name = currDivAControlName
currStatusBarMgr.PostStatusBarUpdate (currDivBControlName)
Debug.Print currDivBControlName
Dim controlDivB As Variant
Set controlDivB = .OLEObjects.Add(ClassType:="Forms.ComboBox.1", _
Link:=False, _
DisplayAsIcon:=False, _
Left:=controlCell.Offset(0, 144).Left, _
Top:=controlCell.Offset(0, 144).Top, _
Width:=140, _
Height:=24)
controlDivB.Name = currDivBControlName
Next
Next
Next
End With
Application.ScreenUpdating = True
End Sub
If I comment out the parts that are intended to create the combo boxes the code runs. I have inspected the Set assignments and they appear to be syntactically correct. I have also made sure the names are unique.
I have let Excel run and run.. All I get is [running]... until I force-quit Excel.
Related
I've been trying to figure this out for past 2 days but I just can't find a proper solution.
Here's my code:
Private Sub Update()
Worksheets("PBF_Graphs").Activate
ThisWorkbook.Worksheets("PBF_Graphs").Unprotect Password:="test"
Dim lastRow As Range
Dim modelSheet As String
Dim currentModel As String
Dim boltNo As Integer
Dim boltRange As Range
Dim BSN_Range As Range
Dim chart As ChartObject
Dim wb As Workbook
Dim ws As Worksheet
Dim chartName As String
Dim graphNo As Integer
Dim chartNo As String
Dim ch As String
Dim i As Integer
Dim j As Integer
Dim currentFixing As String
Set wb = ActiveWorkbook
Set ws = wb.ActiveSheet
Set lastRow = Range("C2")
Set BSN_Range = Range("$C$2")
chartNo = "Chart "
Const fixing1 = "$G$1"
Const fixing2 = "$I$1"
Const fixing3 = "$K$1"
Const fixing4 = "$M$1"
currentFixing = "$G$1"
' set up bolt number as 1 for incrementation always start from FP-FB and finish a RP-RB
boltNo = 1
Set boltRange = Range("G2")
currentModel = MainMenu.globalModel
' set current model name
Select Case currentModel
Case "Model1"
modelSheet = "DP025"
graphNo = 3
j = 3
Case "Model2"
modelSheet = "DP025_2"
graphNo = 7
j = 7
Case "Model3"
modelSheet = "DP025_3"
graphNo = 11
j = 11
Case "Model4"
modelSheet = "DP025_4"
graphNo = 16
j = 16
End Select
For i = graphNo To graphNo + 3
' this will automatically change the chart
ws.ChartObjects(chartNo & j).Activate
ActiveChart.SeriesCollection(1).Select
Application.CutCopyMode = False
ActiveChart.SeriesCollection.NewSeries
ActiveChart.FullSeriesCollection(1).Name = modelSheet & currentFixing
ActiveChart.FullSeriesCollection(1).values = modelSheet & "!" & boltRange.End(xlDown) ' <- doesn't work gives 1004 error
' ActiveChart.FullSeriesCollection(1).values = modelSheet & "!" & "G2:G999" <- works but is not dynamic and if there are blanks it will show in chart
ActiveChart.FullSeriesCollection(1).XValues = modelSheet & BSN_Range.End(xlDown) ' <- doesn't work either
j = j + 1
incrementRange boltNo, boltRange, currentFixing
Next i
ThisWorkbook.Worksheets("PBF_Graphs").Protect Password:="test"
End Sub
Function incrementRange(ByVal bNumber As Integer, bRange As Range, cFixing As String)
bNumber = bNumber + 1
Select Case bNumber
Case 1
Set bRange = Range("$G$2")
cFixing = "$G$1"
Case 2
Set bRange = Range("$I$2")
cFixing = "$I$1"
Case 3
Set bRange = Range("$K$2")
cFixing = "$K$1"
Case 4
Set bRange = Range("$M$2")
cFixing = "$M$1"
End Select
End Function
I commented the part that doesn't work as I want to and what works as I don't want to. Basically the data provided for these charts change, so the range is not fixed and I need to update dynamically based on sample size.
Edit:
The boltRange Range is not working, giving the 1004 application-defined or object-defined error, where as a static string of for example "G2:G999" works, I need that boltRange.End(xlDown) which I want to change according to the required column and go down to the last value
As stated in comments
Changing the following in:
' this will automatically change the chart
ActiveChart.FullSeriesCollection(1).values = modelSheet & "!" & boltRange.End(xlDown) ' <- doesn't work gives 1004 error
' ActiveChart.FullSeriesCollection(1).values = modelSheet & "!" & "G2:G999" <- works but is not dynamic and if there are blanks it will show in chart
ActiveChart.FullSeriesCollection(1).XValues = modelSheet & BSN_Range.End(xlDown) ' <- doesn't work either
To the following:
' this will automatically change the chart
ActiveChart.FullSeriesCollection(1).values = modelSheet & "!" & boltRange.Address & ":" & _
Worksheets(modelSheet).Cells(Rows.Count, boltRange.Column).End(xlUp).Address
ActiveChart.FullSeriesCollection(1).XValues = modelSheet & "!" & BSN_Range.Address & ":" & _
Worksheets(modelSheet).Cells(Rows.Count, BSN_Range.Column).End(xlUp).Address
I'm writing a script that will automatically copy value from sheet FORM and paste to specific sheet that listed on a table of sheet CONFIG with specific column and range for the sheet name and the corresponding column on sheet FORM. However, everytime I turned on the script its cause Not responding, I have tried to change the value that must imported from CONFIG sheet to the real integer but the problem still there.
Sub importvalue()
Dim sheetname, sheetcolumn As String
Dim isheet, iform, output_idx, output_stt As Integer
Dim confsheet As Worksheet
Dim formsheet As Worksheet
Dim outputsheet As Worksheet
Set formsheet = ThisWorkbook.Worksheets("FORM")
Set confsheet = ThisWorkbook.Worksheets("CONFIG")
isheet = 2
Dim thoigian As String
thoigian = formsheet.Range("E1").Value & "-" & formsheet.Range("G1").Value
While isheet <= 5
confsheet.Range("E1").Value = isheet
sheetname = confsheet.Range("J" & isheet).Value
sheetcolumn = confsheet.Range("K" & isheet).Value
Set outputsheet = ThisWorkbook.Worksheets(sheetname)
iform = 2
output_idx = outputsheet.Range("A1").Value
output_stt = outputsheet.Range("C1").Value
outputsheet.Range("A" & output_idx).Value = thoigian
outputsheet.Range("B" & output_idx).Value = output_stt
While iform <= 115
confsheet.Range("E1").Value = iform
outputcolumn = confsheet.Range("D" & iform).Value
formrow = confsheet.Range("C" & iform).Value
If Not formrow = "" Then
outputsheet.Range(outputcolumn & output_idx).Value = formsheet.Range(sheetcolumn & formrow).Value
iform = iform + 1
End If
Wend
isheet = isheet + 1
Wend
End Sub
EDIT 1: This is the dropbox link the for sample that I'm working with: https://www.dropbox.com/s/cjf5xce20o2f0tl/SAMPLE.xlsm?dl=0
EDIT 2: Thank you #FunThomas and #FaneDuru for pointing out my problem.
Sub importvalue()
Dim sheetname, sheetcolumn As String
Dim isheet, iform, output_idx, output_stt As Integer
Dim confsheet As Worksheet
Dim formsheet As Worksheet
Dim outputsheet As Worksheet
Set formsheet = ThisWorkbook.Worksheets("FORM")
Set confsheet = ThisWorkbook.Worksheets("CONFIG")
isheet = 2
Dim thoigian As String
thoigian = formsheet.Range("E1").Value & "-" & formsheet.Range("G1").Value
While isheet <= 5
confsheet.Range("E1").Value = isheet
sheetname = confsheet.Range("J" & isheet).Value
sheetcolumn = confsheet.Range("K" & isheet).Value
Set outputsheet = ThisWorkbook.Worksheets(sheetname)
iform = 2
output_idx = outputsheet.Range("A1").Value
output_stt = outputsheet.Range("C1").Value
outputsheet.Range("A" & output_idx).Value = thoigian
outputsheet.Range("B" & output_idx).Value = output_stt
While iform <= 115
confsheet.Range("E1").Value = iform
outputcolumn = confsheet.Range("D" & iform).Value
formrow = confsheet.Range("C" & iform).Value
If Not formrow = "" Then
outputsheet.Range(outputcolumn & output_idx).Value = formsheet.Range(sheetcolumn & formrow).Value
End If
iform = iform + 1
Wend
isheet = isheet + 1
Wend
End Sub
I was hoping someone would be able to offer me some assistance please? My excel worksheet contains various columns, what I was hoping to do is simply just add another column at the end. So if my worksheet contains 20 columns of data i wish to add header to column U1, next time if my worksheet had 22 columns of data I wish to add header to V1 and so on
Now I have managed to get the next column letter however when I try to pass text into the header row I get an error of Object reference not set to an instance of an object on the following line
.Range(ColumnIndexToColumnLetter(lColumn + 1) & 1).Value = "TESTT"
Any help is greatly appreciated, many thanks
Dim xls As New Excel.Application
Dim xWorkbook As Excel.Workbook
Dim xWorksheet As Excel.Worksheet
Dim lColumn As Long = 0
xWorkbook = xls.Workbooks.Open("D:\Test.xlsx") 'File Location
xWorksheet = xWorkbook.Sheets(1)
xls.Visible = True
With xWorksheet
If xls.WorksheetFunction.CountA(.Columns) <> 0 Then
lColumn = .Columns.Find(What:="*", _
After:=.Range("A1"), _
LookAt:=Excel.XlLookAt.xlPart, _
LookIn:=Excel.XlFindLookIn.xlFormulas, _
SearchOrder:=Excel.XlSearchOrder.xlByColumns, _
SearchDirection:=Excel.XlSearchDirection.xlPrevious, _
MatchCase:=False).Column
Else
lColumn = 1
End If
End With
With xWorksheet
.Range(ColumnIndexToColumnLetter(lColumn + 1) & 1).Value = "TESTT"
End With
Private Function ColumnIndexToColumnLetter(colIndex As Integer) As String
Dim div As Integer = colIndex
Dim colLetter As String = String.Empty
Dim modnum As Integer = 0
While div > 0
modnum = (div - 1) Mod 26
colLetter = Chr(65 + modnum) & colLetter
div = CInt((div - modnum) \ 26)
End While
Return colLetter
End Function
if you use option strict on you have to use cint for conversion, with this change your code works well
With xWorksheet
.Range(ColumnIndexToColumnLetter(CInt(lColumn + 1)) & 1).Value = "TESTT"
End With
my code seems to compile, and run correctly, but for some reason the Inputbox does not seems to be evaluated at all. The Inputbox is in the middle of the code, what I am trying to accomplish is to have a cells data evaluated against a MonthName, and if its a match then spit out a Inputbox to the User. The function it is evaluating is:
=IFERROR(VLOOKUP($A3,'G:\Financial\Facility Work Papers and Financials\1.
Operating Entities\Arbors\2. Financials\2018\5. May\[Arbors May 2018.xls]Trial
Balance'!$A$30:$H$100,8,FALSE),0)
Here is the code:
Sub Date1()
Dim r As Range
Dim s As String
Dim UserInput As String
Dim Curdate As Date
Dim newDate As String
Dim newDate1 As String
Dim newDate2 As String
Dim newDate3 As String
Dim LastCol As Integer
Dim LastRow As Integer
Dim j As Integer
Dim i As Integer
Dim k As Integer
Application.ScreenUpdating = False
Application.DisplayAlerts = False
With ActiveSheet
For k = 1 To 12
Curdate = CDate(k & " " & "01," & " " & "2018")
newDate1 = MonthName(Month(Curdate), False)
newDate2 = MonthName(Month(Curdate), True)
newDate3 = Month(Curdate)
newDate = newDate3 & "." & newDate2
Debug.Print newDate
Debug.Print newDate1
'Defining the loops parameteres
For Each r In ActiveSheet.Range("D3:D6").Cells.SpecialCells(xlCellTypeFormulas)
s = LCase(r.Formula)
If InStr(1, r, newDate1) > 0 Then
UserInput = Application.InputBox(prompt:=newDate1 & "is the current data, if this is the data you want", Title:="please click cancel, otherwise click OK", Default:=newDate1)
End If
Next r
Next k
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
LastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 1 To LastCol
For j = 1 To LastRow
With ActiveWorkbook.Sheets("Data")
.Range(.Cells(j, 1), .Cells(1, i)).Replace What:=oldDate3 & "." & " " & oldDate2, replacement:=newDate, LookAt:=xlPart, MatchCase:=False
.Range(.Cells(j, 1), .Cells(1, i)).Replace What:=oldDate1, replacement:=newDate1, LookAt:=xlPart, MatchCase:=False
End With
Next j
Next i
End With
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Debug.Print Curdate
Debug.Print newDate3 & "." & newDate2
Debug.Print newDate1
Debug.Print newDate
End Sub
I have code that was written for me and it works perfect for four of the entries I am wanting to find, but is there a way that I can add another field value to search for, like ID? I have tried to change the code for what I think would work, but I got errors. I have never coded something this complex, so I do not understand fully what the author did in each section.
Option Explicit
Public Sub ExtractFieldValues()
Const CONSTLAST As Long = 1
Const CONSTFIRST As Long = 2
Const CONSTMIDDLE As Long = 3
Const CONSTRANK As Long = 4
Const TABLEONE As String = "Table 1"
Const FIELDVALUES As String = "FieldValues"
Const LAST_FIRST_MIDDLE As String = "last first middle"
Const FIELDNAMES As String = LAST_FIRST_MIDDLE & " rank"
Const NUMRECORDS As Long = 5 '6
Dim ƒ As Excel.WorksheetFunction: Set ƒ = Excel.WorksheetFunction
Dim ¡ As Long
Dim lrd As Long
Dim nextRowOutput As Long
Dim arngFoundCells(CONSTLAST To CONSTRANK) As Range
Dim varFoundCell As Variant
Dim lngFirstFoundRow As Long
Dim lngNextFoundRow As Long
Dim rngNextFindStart As Range
Dim dictFields As Object
Dim astrFieldNames() As String
Dim astrSplitValues() As String
Dim strFoundValue As String
Dim lngFieldCount As Long
Set dictFields = CreateObject("Scripting.Dictionary")
dictFields.CompareMode = vbTextCompare
With Worksheets
On Error Resume Next
.Add(After:=.Item(.count)).name = FIELDVALUES
On Error GoTo 0
Application.DisplayAlerts = False
If .Item(.count).name <> FIELDVALUES Then
.Item(.count).Delete
.Item(FIELDVALUES).UsedRange.Clear
End If
Application.DisplayAlerts = True
.Item(TABLEONE).Activate
End With
astrFieldNames = Split(" " & FIELDNAMES, " ") ' Force index zero to a blank -> treat as base 1
Set dictFields = CreateObject("Scripting.Dictionary")
For ¡ = CONSTLAST To CONSTRANK
dictFields.Add astrFieldNames(¡), ""
Next ¡
lrd _
= Cells _
.find _
( _
What:="*" _
, After:=Cells(1) _
, LookIn:=xlFormulas _
, Lookat:=xlPart _
, SearchOrder:=xlByRows _
, SearchDirection:=xlPrevious _
) _
.Row
With Range(Rows(1), Rows(lrd))
For ¡ = CONSTLAST To CONSTRANK
Set arngFoundCells(¡) = .find(What:=astrFieldNames(¡), After:=Cells(1))
Next ¡
lngFirstFoundRow _
= ƒ.Min _
( _
arngFoundCells(CONSTLAST).Row _
, arngFoundCells(CONSTFIRST).Row _
, arngFoundCells(CONSTMIDDLE).Row _
)
nextRowOutput = 1
Do
For ¡ = CONSTLAST To CONSTRANK
' Debug.Print arngFoundCells(¡).Address; " ";
dictFields.Item(astrFieldNames(¡)) = ""
Next ¡
' Debug.Print
Select Case True
Case arngFoundCells(CONSTFIRST).Row = arngFoundCells(CONSTMIDDLE).Row:
If arngFoundCells(CONSTRANK).Row <> arngFoundCells(CONSTFIRST).Row Then
Set arngFoundCells(CONSTRANK) = arngFoundCells(CONSTFIRST)
End If
For Each varFoundCell In arngFoundCells
strFoundValue = ƒ.Trim(Replace(varFoundCell.Value2, vbLf, " ")) & " "
If strFoundValue Like "[']*" Then strFoundValue = Mid$(strFoundValue, 2)
If LCase$(strFoundValue) Like astrFieldNames(CONSTLAST) & " " Then
strFoundValue = ƒ.Trim(strFoundValue & Rows(varFoundCell.Row + 1).Cells(1).Value2) & " "
End If
If LCase$(strFoundValue) Like LAST_FIRST_MIDDLE & "*" _
And Len(strFoundValue) - Len(Replace(strFoundValue, " ", "")) < 5 _
Then
strFoundValue = ƒ.Trim(strFoundValue & Rows(varFoundCell.Row + 1).Cells(1).Value2) & " "
End If
astrSplitValues = Split(" " & strFoundValue, " ") ' Force index zero to a blank -> treat as base 1
lngFieldCount = Int(UBound(astrSplitValues) / 2)
For ¡ = 1 To lngFieldCount
dictFields.Item(LCase(astrSplitValues(¡))) = astrSplitValues(¡ + lngFieldCount)
Next ¡
Next varFoundCell
Case Else
Debug.Print " SKIPPED: ";
For ¡ = CONSTLAST To CONSTRANK
Debug.Print arngFoundCells(¡).Address; " ";
Next ¡
Debug.Print
For ¡ = CONSTLAST To CONSTRANK
Debug.Print " "; ƒ.Trim(arngFoundCells(¡).Value2)
Next ¡
Debug.Print
End Select
Sheets(FIELDVALUES).Columns(1).Cells(nextRowOutput).Resize(4).Value _
= ƒ.Transpose(dictFields.Items)
nextRowOutput = nextRowOutput + NUMRECORDS
Set rngNextFindStart = Rows(arngFoundCells(CONSTFIRST).Row + 2).Cells(1)
For ¡ = CONSTLAST To CONSTRANK
Set arngFoundCells(¡) = .find(What:=astrFieldNames(¡), After:=rngNextFindStart)
Next ¡
lngNextFoundRow _
= ƒ.Min _
( _
arngFoundCells(CONSTLAST).Row _
, arngFoundCells(CONSTFIRST).Row _
, arngFoundCells(CONSTMIDDLE).Row _
)
Loop While lngNextFoundRow <> lngFirstFoundRow
End With
End Sub