I have to display an excel sheet on a ListBox in VB.net. So far I have been able to display the first column. I have used zones to format two columns. Here is my code for this part:
Dim fmtStr As String = "{0,15}, {1,15}"
I then have this code for displaying in the ListBox:
For i = 0 To AscW(ListBox1.Items.Count.ToString()(i = i + 1)) - 1
cella = "A" & Convert.ToString(i + 1)
cellb = "B" & Convert.ToString(i + 1)
cella = aSheet.Range(cella).Value
cellb = aSheet.Range(cellb).Value
ListBox1.Items.Add(String.Format(fmtStr, "1", "2"))
If cella = "" Or cellb = "" Then
ListBox1.Items.Add("")
Else
ListBox1.Items.Add(String.Format(fmtStr, cella, cellb))
End If
I'm getting errors while running.. Is there another method to display the columns side by side ?
UPDATE: Everything is working fine but the output is messy like so:
Image
ListBox1.Font = New System.Drawing.Font("Consolas", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Related
Having some trouble figuring this out. Here is the goal:
let's say I import some values with the following columns (Products, Category, Mobile, Desktop, Tablet)
Imported Values
Let's say I have a naming convention that is defined (doesn't change)
Hard defined naming convention (used for concatenation)
After clicking a button, my VBA code should be able to concatenate the Products values with the defined naming convention, based on the Yes/No criteria. Here is what I'm trying to get as the end result:
End goal after clicking VBA button
Tried a ton of things, here is an example of what I tried for Products of Category = Chocolate:
Code in Text Format:
'Example Attempt for Products that are of Category = Chocolate'
Dim chocolateOption1 As String 'Chocolate Mobile'
Dim chocolateOption2 As String 'Chocolate, Desktop'
Dim chocolateOption3 As String 'Chocolate, Tablet'
startingLine = 2 'This is just starting from the second row'
With SavedData 'Name of the open worksheet'
Do While .Cells(startingLine, 2) <> ""
If .Cells(startingLine, 2) = "Chocolate" And
.Cells(startingLine, 3) = "Yes" Then
chocolateOption1 = .Cells(startingLine, 1) &
.Cells(10,7) 'Concatenation to generate
DarkChocolate_Chocolate_Mobile'
ElseIf .Cells(startingLine, 2) = "Chocolate" And
.Cells(startingLine, 4) = "Yes" Then
chocolateOption2 = .Cells(startingLine, 1) &
.Cells(11,7) 'Concatenation to generate
DarkChocolate_Chocolate_Desktop'
ElseIf .Cells(startingLine, 2) = "Chocolate" And .Cells(startingLine, 5) = "Yes" Then
chocolateOption3 = .Cells(startingLine, 1) & .Cells(12,7)
'Concatenation to generate DarkChocolate_Chocolate_Tablet'
Else
chocolateOption1 = "Please import a valid Category and try again…"
chocolateOption2 = ""
chocolateOption3=""
End If
.Cells(startingLine, 6) = chocolateOption1
.Cells(startingLine, 6) = chocolateOption2
.Cells(startingLine, 6) = chocolateOption3
startingLine = startingLine + 1 'move to the next line once Dark
Chocolate product asset names are all generated and complete'
Loop
End With
Thank you for your help!
As part of a larger process I need to create a Excel VBA Macro that read the values from a column and applies basic formatting to the row based on the values in each cell.
The spreadsheet itself is exported from another program and opens directly in Excel. All columns come across formatted as General
The sequence is this:
Start at the second row in Sheet1
Look at Column J
Read the RGB value (which is shown as RGB(X,Y,Z) where X, Y, and Z are the numerical values for the color that needs to be used)
Change that rows text Color for Column A-I to that color
Continue through all rows with text
I found this thread, but I'm not able to make it work.
Any help here much appreciated.
Sub ColorIt()
Set cl = Cells(2, "J")
Do Until cl = ""
txt = cl.Value2
cl.Offset(, -9).Resize(, 9).Font.Color = _
Evaluate("SUMPRODUCT({" & Mid(txt, 5, Len(txt) - 5) & "},{1,256,65536})")
Set cl = cl.Offset(1)
Loop
End Sub
Result:
Edit2
Sub ColorIt2()
Const RGB_COL = "M"
Set cl = Cells(2, RGB_COL)
Do Until cl = ""
txt = cl.Value2
cl.Offset(, 1 - cl.Column).Resize(, cl.Column - 1).Interior.Color = _
Evaluate("SUMPRODUCT({" & Mid(txt, 5, Len(txt) - 5) & "},{1,256,65536})")
Set cl = cl.Offset(1)
Loop
End Sub
Please, use the next function. It will convert the string in a Long color (based on the RGB three parameters. It will work for both caser of comma separators ("," and ", "):
Function ExtractRGB(strRGB As String) As Long
Dim arr: arr = Split(Replace(strRGB, " ", ""), ",")
ExtractRGB = RGB(CLng(Right(arr(0), Len(arr(0)) - 4)), CLng(arr(1)), CLng(left(arr(2), Len(arr(2)) - 1)))
End Function
It can be tested in the next way:
Sub TestExtractRGB()
Dim x As String, color As Long
x = "RGB(100,10,255)"
x = "RGB(100, 10, 255)"
color = ExtractRGB(x)
Debug.Print color
Worksheet("Sheet1").Range("A2:I2").Font.color = color
'or directly:
Worksheet("Sheet1").Range("A2:I2").Font.color = _
ExtractRGB(Worksheet("Sheet1").Range("J2").value)
End Sub
If you comment x = "RGB(100, 10, 255)", it will return the same color valuem for the previous x string...
If you need to do it for all existing rows, the code must iterate from 2 to last Row and you only need to change "A2:I2" with "A" & i & ":I" & i
If necessary, I can show you how to deal with it, but I think is very simple...
I have been following a tutorial and playing with developing Fully Automated User Data Entry Form.
The Video I followed PSB:
https://www.youtube.com/watch?v=P53T6oxgUVA&ab_channel=TheDataLabs
After having downloaded his files and gone through the code step by step, I still get a yellow highlight over the particular section of the ELSE statement on the Sub Clear()? Additionally my Listbox does not update correctly it replaces the previous entry with the next entry saved.
Please view the code below
Sub Clear()
Dim iRow As Long
iRow = [Counta(Room Access 2!A:A)] '''Identify Last Row as per tutorial''
With Frm_Room_Access_2
.Surname_Txtbx.Value = ""
.Rank_Cmbobx.Value = ""
.Section_Txtbx.Value = ""
.Extention_Txtbx.Value = ""
.Service_Number_Txtbx.Value = ""
.Due_Time_Txtbx.Value = ""
.OpBtn_Time_as_Now.Value = False
.SerialNo_Txtbx.Value = ""
If iRow > 1 Then
.List_Database.RowSource = "Room Access 2!A2:G" & iRow
Else
.List_Database.RowSource = "Room Access 2!A2:G" '''' Keeps Highlighting Error ''''
End If
End With
End Sub
Sub Submit()
Dim sh As Worksheet
Dim iRow As Long
Set sh = ThisWorkbook.Sheets("Room Access 2")
If Frm_Room_Access_2.SerialNo_Txtbx.Value = "" Then
iRow = [Counta(Room Access 2!A:A)] + 1
Else
iRow = Frm_Room_Access_2.SerialNo_Txtbx.Value
End If
With sh
.Cells(iRow, 1) = iRow - 1
.Cells(iRow, 2) = Frm_Room_Access_2.Surname_Txtbx.Value
.Cells(iRow, 3) = Frm_Room_Access_2.Service_Number_Txtbx.Value
.Cells(iRow, 4) = Frm_Room_Access_2.Rank_Cmbobx.Value
.Cells(iRow, 5) = Frm_Room_Access_2.Section_Txtbx.Value
.Cells(iRow, 6) = Frm_Room_Access_2.Extention_Txtbx.Value
.Cells(iRow, 7) = IIf(Frm_Room_Access_2.OpBtn_Time_as_Now.Value = True And Frm_Room_Access_2.Due_Time_Txtbx.Value = "", Now(), Frm_Room_Access_2.Due_Time_Txtbx.Value)
End With
End Sub
The Issues that I want to resolve is firstly why is my list box not adding another entry under the first entry, it simply replaces the first entry upon saving. Do I require another method for this?
Secondly why is the code Highlighting that the Else Statement is incorrect despite following the tutorial and replicating the code correctly to my particular needs.
Any advice will be greatly appreciated.
The last cell with text in a column can be found with
Range("B" & 1).End(xlDown).Offset(1, 0).Select
The Offset(1, 0) selects the cell below in order for you to copy data to it.
To select a worksheet use:
Worksheets("Room Access 2").Select
No underlines are needed.
I am trying to transfer number of content controls to excel spreadsheet. The issue is, I would only like to transfer words which were selected by used with bold. I would appreciate any help.
I have already tried to adopt one of your workflows but I am stuck.
Dim txtboxstring As Characters
Dim CharacterFont As Font
Dim counter As Integer
(...)
ElseIf C_C.Title = "Data_Sheet_Structural_Type" Then
Workbooks(DES_Bridges_WrkBook).Sheets(DES_Bridges_WrkSheet).Cells(i, 17).Value = C_C.Range.Text
'Test for bold text
ElseIf C_C.Title = "Data_Sheet_Material" Then
Set textboxstring = C_C.Range.Characters
For counter = 1 To textboxstring.Count
CharacterFont = textboxstring.Font
If CharacterFont.Bold Then
BoldFound = BoldFound & txtboxstring(counter)
End If
Next counter
Workbooks(DES_Bridges_WrkBook).Sheets(DES_Bridges_WrkSheet).Cells(i, 18).Value = BoldFound
BoldFound = ""
ElseIf C_C.Title = "Deck_Width" Then
Workbooks(DES_Bridges_WrkBook).Sheets(DES_Bridges_WrkSheet).Cells(i, 19).Value = C_C.Range.Text
(...)
I get following error:
Objec doesnt support Property or Method
in line
CharacterFont = textboxstring.Font
I would appreciate any help.
I was given the assignment of parsing a word document form, and putting it into an Excel sheet. The code below does that with ease.. but I've recently learned I need to put that data into an Excel table instead of just the cells of a sheet.
For row = 1 To theTable.Rows.Count `until end of rows
isHeaderOrNot = theTable.Cell(row, 0).Range.Text `if first field in row is a header
If isHeaderOrNot.Contains("Section") Or isHeaderOrNot.Contains("Field") Then Continue For
keyText = theTable.Cell(row, 2).Range.Text `gets key text
valueText = theTable.Cell(row, 3).Range.Text `gets value text
cleanStringKey = Regex.Replace(keyText, Chr(7), "") `clean strings
cleanStringValue = Regex.Replace(valueText, Chr(7), "")
xlWorkSheet.Cells(1, column) = cleanStringKey `puts key in row 1 and column n
xlWorkSheet.Cells(2, column) = cleanStringValue `puts value in row 2 column n
column = column + 1 `increment column
Next
I was wondering if I would have to completely change my code in order to make it a table... In short go from
To
I am completely new to VB.net so if you could, dumb everything down as much as possible.
You can use Add method of WorkSheet.ListObject to create a new list (table).
Example
After adding reference to Microsoft.Office.Interop.Excel Add this import to the form:
Imports XL = Microsoft.Office.Interop.Excel
Then use such code to create a list:
Dim Application = New XL.Application()
Application.Visible = True
Dim book = Application.Workbooks.Add()
Dim sheet = DirectCast(book.Worksheets(1), XL.Worksheet)
sheet.Cells(1, 1) = "Product"
sheet.Cells(1, 2) = "Price"
sheet.Cells(2, 1) = "Product 1"
sheet.Cells(2, 2) = "100"
sheet.Cells(3, 1) = "Product 2"
sheet.Cells(3, 2) = "200"
sheet.Cells(4, 1) = "Product 3"
sheet.Cells(4, 2) = "300"
Dim list = sheet.ListObjects.Add( _
XL.XlListObjectSourceType.xlSrcRange, _
sheet.Range(sheet.Cells(1, 1), sheet.Cells(4, 2)), _
Type.Missing, XL.XlYesNoGuess.xlYes)
Then you will see this result: