Hello everyone and thank you!
I have some wierd exception that I cannot solve.
It is not my first program though I cannot figure it out!
I'm writing an application wich through it I want to fill an excel workbook.
every time i'm trying to merge cells, I'm recieving an error.
I have checked over and over and I just can't figure it out.
everything in my code supposed to work.
the xl application,workbook,worksheet and ranges all declared.
the wierdest is that if during the code running, i'm entering the excel worksheet manualy (after pausing the program with a msgbox or something), and
selecting some cell (no matter wich), the code is running just fine :(((
Im not aloud to add in images so here is the code - copy paste.
the btn click code:
Private Sub btnCreatPriceQuote_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCreatPriceQuote.Click
oXL.Visible = False
Dim indexForCopy As Integer
'few commands for faster run in the xl WB
XlFasterRun()
'Replace pricequote sheet with its format to avoid overwrite
PriceQuoteRenew()
'Decalre rows for add details on the BAMAY
DecalreFirstRow()
FillPriceQuoteTable()
TillIndex = 39
EmptyCellIndex = 13
the declaration from a module:
`Public Sub DecalreFirstRow()
rng1thcol1thLinePQ = shPriceQuote.Range("A75:D75")
rng2thcol1thLinePQ = shPriceQuote.Range("E75:F75")
rng3thcol1thLinePQ = shPriceQuote.Range("G75:H75")
rng4thcol1thLinePQ = shPriceQuote.Range("I75:J75")
rng5thcol1thLinePQ = shPriceQuote.Range("K75:L75")
rng6thcol1thLinePQ = shPriceQuote.Range("M75:N75")
rng7thcol1thLinePQ = shPriceQuote.Range("O75:T75")
rng8thcol1thLinePQ = shPriceQuote.Range("U75:X75")
rng1thcol1thLineCF = shCusFile.Range("A12:G12")
rng2thcol1thLineCF = shCusFile.Range("H12:K12")
rng3thcol1thLineCF = shCusFile.Range("L12:O12")
rng4thcol1thLineCF = shCusFile.Range("P12:S12")
rng5thcol1thLineCF = shCusFile.Range("T12:W12")
rng6thcol1thLineCF = shCusFile.Range("X12:AA12")
rng7thcol1thLineCF = shCusFile.Range("AD12:AI12")
rng8thcol1thLineCF = shCusFile.Range("AJ12:AN12")
End Sub
`
public sub where the exception occured:
Public Sub FillPriceQuoteTable()
With rng1thcol1thLinePQ
.Select()
.Value = rng1thcol1thLineCF.Value
**.Merge()**
End With
With rng2thcol1thLinePQ
.Select()
**.Merge()**
.Value = rng2thcol1thLineCF.Value
End With
With rng3thcol1thLinePQ
.Select()
**.Merge()**
.Value = rng3thcol1thLineCF.Value
End With
With rng4thcol1thLinePQ
.Select()
**.Merge()**
.Value = rng4thcol1thLineCF.Value
End With
Replace:
rng.Merge()
With:
rng.mergecells=true.
Related
Apologies any incorrect terms, this is the first time I am trying to code a macro. I currently have the following code running:
Private Sub Worksheet_Deactivate()
'Alpha Show / Hide
If Sheets("Project_selection").Range("D4") = Range("C2") Then
Sheet3.EnableCalculation = True
ElseIf Sheets("Project_selection").Range("D4") = "All" Then
Sheet3.EnableCalculation = True
Else
Sheet3.EnableCalculation = False
End If
End Sub
which has been cobbled together from other codes and google. It works, but only when I move out of the sheet, which I think is being driven by the first line.
I would actually like it to activate when the Cell D4 in the 'Project_selection' sheet (a separate sheet to the one the code is on) gets changed - does anyone know how I would do that? I have seen references to worksheet_change, but I do not understand how one defines the target/range to get the appropriate reference.
Hope that makes sense and thanks in advance!
If you were to place the following code under the sheet (Project_selection), it would fire that event every time a change has happened in Cell D4:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet: Set ws = Sheets("Project_selection")
If Target.Address = "$D$4" Then
If ws.Range("D4") = ws.Range("C2") Then
Sheet3.EnableCalculation = True
ElseIf ws.Range("D4") = "All" Then
Sheet3.EnableCalculation = True
Else
Sheet3.EnableCalculation = False
End If
End If
End Sub
I have this sub/macro that works if I run it as BeforeRightClick. However, I would like to change it so I can actually use my rightclick and put the macro on a button instead.
So I have tried to change the name from BeforeRightClick.
I have tried with both a normal form button and an ActiveX.
All this + some more code is posted under Sheet1 and not modules
Dim tabA As Variant, tabM As Variant
Dim adrA As String, adrM As String
' Set columns (MDS tabel) where data should be copied to (APFtabel)
'Post to
'P1-6 divisions ' Name adress, etc
Const APFtabel = "P1;P2;P3;P4;P5;P6;E9;E10;E13;E14;E23;N9;N10;N11;N12;N20"
'Load data from
Const MDStabel = "N;O;P;Q;R;S;H;Y;Z;AB;W;AF;T;D;AA;V;"
Dim APF As Workbook
' APFilNavn is the name of the AP form
Const APFilNavn = "APForm_macro_pdf - test.xlsm"
' Const APFsti As String = ActiveWorkbook.Path
Const APFarkNavn = "Disposition of new supplier"
' APsti is the path of the folder
Dim sysXls As Object, APFSti As String
Dim ræk As Integer
Private Sub CommandButton1_Click()
APFormRun
End Sub
' Here I changed it from BeforeRightClick
Private Sub APFormRun(ByVal Target As Range, Cancel As Boolean)
Dim cc As Object
If Target.Column = 8 Then
APFSti = ActiveWorkbook.Path & "\"
If Target.Address <> "" Then
For Each cc In Selection.Rows
Cancel = True
ræk = cc.Row
Set sysXls = ActiveWorkbook
åbnAPF
overførData
opretFiler
APF.Save
APF.Close
Set APF = Nothing
Set sysXls = Nothing
Next cc
End If
End If
End Sub
Private Sub overførData()
Dim ix As Integer
tabA = Split(APFtabel, ";")
tabM = Split(MDStabel, ";")
Application.ScreenUpdating = False
For ix = 0 To UBound(tabM) - 1
If Trim(tabM(ix)) <> "" Then
adrM = tabM(ix) & ræk
If tabA(ix) <> "" Then
adrA = tabA(ix)
End If
With APF.ActiveSheet
.Range(adrA).Value = sysXls.Sheets(1).Range(adrM).Value
End With
End If
Next ix
End Sub
Private Sub opretFiler()
' Here I run some other macro exporting the files to Excel and PDF
btnExcel
btnExportPDF
End Sub
if you put this code in Sheet1, then to access it from a button you need to define its name (in the button) as Sheet1.APFormRun (and I think you need to make it Public).
If you move the sub and everything it calls to a Module (after doing an Insert->Module), then you do not need the Excel Object Name prefix.
A very detailed write-up about scoping is at the link below. Scroll down to the "Placement of Macros/ Sub procedures in appropriate Modules" section: http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=162:excel-vba-calling-sub-procedures-a-functions-placement-in-modules&catid=79&Itemid=475
In your code above, I had to comment out all the subs you didn't include just to get it to compile for debugging.
To make a sub accessible to the Macros button or to "Assign Macro..." you have to make it Public
Also to make a sub accessible, it cannot have any passed parameters.
So you will have to remove the passed parameters from the Public Sub APFormRun() definition
Therefore you will have to re-write the initial portion of APFormRun ... currently your APFormRun relies upon getting a passed parameter (Target) of the selected cell that you right-clicked upon. When you press a button, there is no cell that you are right-clicking upon. It is not a cell-identifying Excel event. You will have to obtain the selected cell via the Selection excel object. There are a lot of StackOverflow answers on how to do that.
I just discover problem with my code, which not appears yesterday.
This is code from "Sheet1 (Calculator)":
Option Explicit
Private Sub Worksheet_Activate()
Sheets("Calculator").ComboBox1.ListFillRange = "Materials!B4:B7"
Sheets("Calculator").ComboBox1.ListIndex = 0
End Sub
Private Sub ComboBox1_Change()
Sheets("Calculator").Range("T18") = ComboBox1.ListIndex + 1
Select Case Sheets("Calculator").ComboBox1.ListIndex
Case 0
Sheets("Calculator").ComboBox2.ListFillRange = "Materials!G4:G5"
Sheets("Calculator").ComboBox2.ListIndex = 0
Case 1
Sheets("Calculator").ComboBox2.ListFillRange = "Materials!G6"
Sheets("Calculator").ComboBox2.ListIndex = 0
Case 2
Sheets("Calculator").ComboBox2.ListFillRange = "Materials!G7:G10"
Sheets("Calculator").ComboBox2.ListIndex = 0
Case 3
Sheets("Calculator").ComboBox2.ListFillRange = "Materials!G11:G12"
Sheets("Calculator").ComboBox2.ListIndex = 0
End Select
End Sub
Everything working fine, while Excel is opened. But if I save and close Excel and then reopen it, code breaks at first line under "Case 0" with error message:
Run-time error '438'
Object doesn't support this property or method
Then when I stop debuging and change item in ComboBox1, code works fine again and ComboBox2 is filled with correct data. Do you have any ideas where can be problem?
Here is the file.
Dont know if you initialized your ComboBox object properly.....but this should work:
Set ComboBox1 = Sheets("Calculator").Shapes(1)
ComboBox1.ControlFormat.ListFillRange = "Materials!B4:B7"
so mayb its because mainly you are missing the ControlFormat thing!?
Private Sub ComboBox1_Change()
with Sheets("Calculator")
.Range("T18") = ComboBox1.ListIndex + 1
if .ComboBox1.ListIndex > -1 Then .ComboBox2.List=sheets("Materials").range(G4:G5").offset(choose(.ComboBox1.ListIndex+1).value
end with
End Sub
I am trying to make it so that if check box 82 is not checked then cell J39 is left blank but if it is checked the value is 3.8. On the line "Sheet.Range("J39").PasteSpecial Paste = "3.8"" I am getting a error message 1004
Sub ChckBx_Deisel_Engines()
Dim Sheet As Worksheet: Set Sheet = ThisWorkbook.Worksheets("NSR FORM")
If Sheet.Shapes("Check Box 82").ControlFormat.Value = 0 Then
Sheet.Range("J39").PasteSpecial Paste = ""
End
ElseIf Sheet.Shapes("Check Box 82").ControlFormat.Value = 1 Then
Sheet.Range("J39").PasteSpecial Paste = "3.8"
End If
End Sub
Any Ideas on how to perform this function in a better way would also begreatly appreciated
Figured it out! Here is the code I changed it to to get this function to work
Sub Button82_Click()
If ThisWorkbook.Worksheets("NSR FORM").Shapes("Check Box 82").OLEFormat.Object.Value = 1 Then
Range("J39").Value = "3.8"
Else
Range("J39").Value = ""
End If
End Sub
Dim Sheet As Worksheet: Set Sheet = ThisWorkbook.Worksheets("NSR FORM")
If Sheet.Shapes("Check Box 82").ControlFormat.Value = 0 Then
Sheet.Range("J39").PasteSpecial Paste = "" :Exit sub <<< to exit direct from your project
ElseIf Sheet.Shapes("Check Box 82").ControlFormat.Value = 1 Then
Sheet.Range("J39").PasteSpecial Paste = "3.8"
End If
End Sub
I am VERY new to VBA (and know of it only within excel).
I am trying to cycle through some (but not all) checkboxes. They are currently named CheckBox1 through CheckBox15. Howe do I cycle through say for instance CheckBox5 through CheckBox10?
I guess I am hoping there is a 'method' similar to 'CheckType' for controls that will allow me to check name?
Here is what I have tried. Causes a Compile Error - Sub or Function not defined, and highlights Worksheet.
Private Sub BoxCheck()
atLeastOneChecked = False
For i = 2 To 4
If Worksheets("ActiveX").Controls("Checkbox" & i).Value = True Then
atLeastOneChecked = True
End If
Next i
End Sub
While the above doesnt work, what is below does:
Private Sub BoxCheck()
atLeastOneChecked = False
For i = 1 To 2
If Sheet2.CheckBox2.Value = True Then
atLeastOneChecked = True
End If
Next i
End Sub
Of course, the loop has no affect on the outcome, but it compiles and atLeastOneChecked turns from False to True when Checkbox2 is True. Note that Sheet2 has been named ActiveX. I clearly don't understand how Worksheet and Controls work. Could someone help?
After fixing the error described below, this still won't work. I simplified to the following:
Private Sub BoxCheck()
Dim ole As OLEObject
atLeastOneChecked = False
Set ole = Sheets("ActiveX").OLEObjects("Checkbox2")
If ole.Value = True Then
atLeastOneChecked = True
End If
End Sub
This doesn't work. It fails at:
If ole.Value = True Then
The error states: Object Doesn't support this property or method
This is true for OLEObjects, but not for Checkboxes. When I look at the properties of ole, I see that its Object property is set to Object/Checkbox and that this Object has a value. I guess that is what I should be referencing in my if statement, but I don't know how.
I think I solved the problem.
The value of the Checkbox is accessed by Referencing the Object property within the OLEObject I set...Like this:
If ole.Object.Value = True Then
Thanks for all your help. If someone has a more elegant solution, I would still like to see it.
Use CheckBox.Name
Example:
For Each cb In ActiveSheet.CheckBoxes
If cb.Name = "CheckBox5"
' Do stuff
End If
Next cb
To expand on #Parker's answer:
Private Sub BoxCheck()
atleastonechecked = False
Dim oles As OLEObject
For i = 2 To 4
'If you're using Shapes Controls:
If ThisWorkbook.Worksheets("ActiveX").Shapes("Check Box " & i).Value = True Then
atleastonechecked = True
End If
' If you're using ActiveX Controls
Set oles = ThisWorkbook.Worksheets("ActiveX").OLEObjects("CheckBox" & i)
If oles.Value = True Then
atleastonechecked = True
End If
Next i
End Sub
Sorry, just put together the previous answer without testing - that always fails. Just use the If loop based on what type of control you're using.