How to fill a pattern to excel cell?
Based on few posts on this forum, I tried to use following, but no success so far.
a)
oWB1.Worksheets(i).Cells(7, 3).Interior.PatternIndex = 5
b)
Dim style As Microsoft.Office.Interop.Excel.Style
style = oWB1.Styles.Add("Style1")
style.Interior.Pattern = Microsoft.Office.Interop.Excel.XlPattern.xlPatternSolid
oWB1.Worksheets(i).Cells(7, 3).Style = "Style1"
Any help will be really appreciated.
Thanks
This (Excel VSTO) code works:
Dim rng As Range = activeSheet.Range(1,1)
rng.Style = "Calculation"
"Calculation" is the name of the style.
Related
I have this code for a textbox.
Dim TextBox15 As Range
ActiveDocument.Variables("bmagicf").Value = Me.TextBox15.Value
Me.TextBox15.Value = Me.TextBox15.Text
With ActiveDocument
.Fields.Update
End With
How can I apply a style to it in the same sense as the code below?
Dim ComboBox9 As Range
Set ComboBox9 = ActiveDocument.Bookmarks("bmagich").Range
ComboBox9.Text = Me.ComboBox9.Value
If Me.ComboBox9.Value = "No" Then
ComboBox9.Text = "Appendices"
ComboBox9.Style = ActiveDocument.Styles("Style 22")
End If
I tried the code below but had no luck:
Dim TextBox15 As Range
ActiveDocument.Variables("bmagicf").Value = Me.TextBox15.Value
Me.TextBox15.Value = Me.TextBox15.Text
Textbox15.Style = ActiveDocument.Styles("Style 22")
With ActiveDocument
.Fields.Update
End With
Your question is extremely misleading, and your code is confusing.
From looking at your code it appears that you are not trying to apply a Style to a text box on a User Form, though the title of your question says you are.
Your code is confusing because you have used the same name for several different things.
Your code isn’t working because you have missed a line of code. Look again at the combo box example. The second line is crucial.
Set ComboBox9 = ActiveDocument.Bookmarks("bmagich").Range
You need a line of code like that for your text box example, otherwise the Range variable, TextBox15, doesn’t point to anything.
I'm using three Workbooks. My Current workbook, The database workbook (DB_Wkb), the Document to change ( Doc_Wkb) and my current macro file.
I'm using vLookup to compare the ID, and get the name from the database The problem is that it works fine, but it takes a lot of time and Excel stops responding. I believe the use of vlookup is what makes my macro to take so long.
Dim Doc_Wkb As Workbook 'Document
Dim DB_Wkb As Workbook 'Database
Set Doc_Wkb = Workbooks.Open(Doc_Path)
Doc_Wkb.Worksheets(Sheet_Name).Cells.Select 'sheet_name=Sheet of the Document
Selection.UnMerge
Doc_Wkb.Worksheets(Sheet_Name).Range("A5:S" & Cells(Rows.Count, "S").End(xlUp).Row).RemoveDuplicates Columns:=16, Header:=xlYes
Set DB_Wkb = Workbooks.Open(DB_Path)
Dim Str As String
Dim Cont_Doc As Double
P = 6 ' P Declared in Module
Cont_DB = DB_Wkb.Worksheets(Sheet_name_2).Range("B:F") ' Sheet_name_2 = sheetname of DB
While Not IsEmpty(Doc_Wkb.Worksheets(Sheet_Name).Cells(P, 5))
Cont_Doc = Doc_Wkb.Worksheets(Sheet_Name).Cells(P, 5)
store = Application.VLookup(Cont_Doc, Cont_DB, 5, False)
Doc_Wkb.Worksheets(Sheet_Name).Cells(P, 20) = store
P = P + 1
Wend
Thank you so much for your help.
Update: I figured out an alternative. Using DoEvents solves this problem. Meanwhile a progress bar can be used for the looks.
I start my adventure with VBA. I would like to create formula on VBA, use vlookup but something is going wrong with this.
Also I would like to implement vlookup for cells, when
cells from deferent column will be filled
( for example if WB_WS_Pricing.Range("A4")<>0 then
WB_WS_PRICING.Range("CX4") = "=IFNA(VLOOKUP(Delivering!E4,DATA!A:I,9,0),"")"
Sub formula()
Set WB_CMSO_MASS_IBERIA = ThisWorkbook
Set WB = ThisWorkbook
Set WB_WS_PRICING = WB.Sheets("Pricing")
Set WB_WS_HEADER = WB.Sheets("Header")
Set WB_WS_DATA = WB.Sheets("DATA")
Set WB_WS_Extension = WB.Sheets("Extension")
Set WB_WS_DELIVERING = WB.Sheets("Delivering")
WB_WS_PRICING.Range("CX4") = "=IFNA(VLOOKUP(Delivering!E4,DATA!A:I,9,0),"")"
End Sub
Enyone has idea what is wrong?? For me the formula seems be fine...
You need to escape the double quotes in your formula with an extra quote in front of each (ie """" not "")
WB_WS_PRICING.Range("CX4") = "=IFNA(VLOOKUP(Delivering!E4,DATA!A:I,9,0),"""")"
I am new to VBA programming and working through excel VBA programming by myself and have a question involving multiple ComboBoxes that are being used to determine the value of a single cell.
I have 2 ComboBoxes.
Right now I am using something close to this syntax
Private Sub ComboBox1_Change()
If Me.ComboBox1.Value = "A" and Me.ComboBox2.Value = "2" Then
Range("A2").Value = Range ("B6").Value
Else Range("A2").Value = Range("B7")
End If
The code is not working. I am not getting anything return in what would be A2.
I am assuming that the entries in the ComboBoxes are strings of any sort and are correctly put into the code.
Any more troubleshooting help would be much appreciated.
You might try to capture the combobox2 value in a string variable
dim str_comboBox2 as string
str_comboBox2 = Me.ComboBox2.value
If Me.ComboBox1.value = "A" and str_comboBox2 ="2" then
As it might be interpreting your entry as a number thus not satisfying the IF statement.
Or change to:
If Me.ComboBox1.value = "A" and Me.ComboBox2.value = 2 then
In recorded VBA macros, it seems that their formulas use R1C1 reference style. For instance, to fill in B4 with B2+1:
Range("B4").Select
ActiveCell.FormulaR1C1 = "=R[-2]C+1"
Does anyone know if it is possible to switch off this mode? For instance, let recorded macro look like:
Range("B4").Select
ActiveCell.Formula = "=B2+1"
I believe you cannot do that. The macro will always record in R1C1 style.
You can always switch the style but it will only be applied to the worksheet and if you record a macro it will still show R1C1 reference style.
It is very easy to understand the R1C1 style
In R1C1 reference style, the range is referred by how far the cells are located from the cell you are calling. For example, if you have 5 values from R1C1 to R5C1 and the range is called from R7C2, then the range would be R[-6]C[-1]:R[-2]C[-1]. Here the first cell in the range is 6 rows before the cell R7C2 and 1 column before the cell R7C2 and similarly for the last cell in the range.
If I take your example then "=R[-2]C+1" means that the formula is referring to a row which is two rows up (-2) and in the same column (0). Your formula is same as "=R[-2]C[0]+1"
EDIT
Here is a small function that I wrote which can help you convert R1C1 to A1 string
Sub Sample()
'~~> This will give you $B$2
Debug.Print R1C12A1("B4", "R[-2]C")
'~~> This will give you E227
Debug.Print R1C12A1("O9", "R[218]C[-10]", True)
'~~> This will give you $Y$217
Debug.Print R1C12A1("O9", "R[208]C[10]")
End Sub
Function R1C12A1(baseCell As String, sRC As String, Optional RemDollar As Boolean = False) As String
Dim MyArray() As String
Dim r As Long, c As Long
sRC = Replace(sRC, "R", "")
If Left(sRC, 1) = "C" Then
r = 0
Else
r = Replace(Replace(Split(sRC, "C")(0), "[", ""), "]", "")
End If
If Right(sRC, 1) = "C" Then
c = 0
Else
c = Replace(Replace(Split(sRC, "C")(1), "[", ""), "]", "")
End If
If RemDollar = False Then
R1C12A1 = Range(baseCell).Offset(r, c).Address
Else
R1C12A1 = Replace(Range(baseCell).Offset(r, c).Address, "$", "")
End If
End Function
Note: I have not done any error handling here. I am sure you can incorporate that if needed.
There used to be a facility to toggle relative reference when recording a macro.
When you have started recording, in the macro toolbar - near the stop button - there was a button to toggle relative reference; is this not the same as toggling R1C1 ? or isn't this available anymore?
I never bothered toggling it myself as like Siddharth says the R1C1 isn't too tricky to understand plus, irrespective of whatever you do, the VBA will need some editing so at the same time if you wish to use other syntax it's easy enough to change.
I've just played around with the following but it doesn't seem to help so maybe I'm mixing up the use of this button with R1C1...