I've a worksheet with many cells formated as currency and i want modify the currency format through a combobox,
First i use this code to get the inicial currency type/format,
Private Sub ComboBox1_DropButtonClick()
inicial = Me.ComboBox1.Value
Select Case inicial
Case "EUR"
oldFormat = "#.##0 €"
Case "GBP"
oldFormat = "[$£-809]#.##0"
Case "USD"
oldFormat = "#.##0 [$USD]"
End Select
End Sub
The oldformat variable is a global variable,
Public oldformat As String
After that i want to do a find using oldformat variable and a replace using a newformat variable,
Private Sub ComboBox1_Change()
Dim ws As Worksheet
Dim newFormat As String
'On Error Resume Next
newValue = Me.ComboBox1.Value
Select Case newValue
Case "EUR"
newFormat = "#.##0 €"
Case "GBP"
newFormat = "[$£-809]#.##0"
Case "USD"
newFormat = "#.##0 [$USD]"
End Select
'Set rNextCell = Application.FindFormat
For Each ws In ActiveWorkbook.Worksheets
Application.FindFormat.Clear
Application.FindFormat.NumberFormat = oldFormat
Application.ReplaceFormat.Clear
Application.ReplaceFormat.NumberFormat = newFormat
ws.Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True
Next ws
End Sub
I read the new value through the user choice on the combobox.
But this not work at all, the variables oldformat and newformat receive the correct values but i got a error on,
Application.FindFormat.NumberFormat = oldformat
Application.ReplaceFormat.NumberFormat = newFormat
Run-time error '1004': Application-defined or object-defined error
Is there any way to pass the newformat and oldformat value to the Numberformat property?
Or someone have another away to do this?
Link for exemple file,
https://www.dropbox.com/s/sdyfbddxy08pvlc/Change_Currency.xlsm
I apreciate any help, i m a little bit new on VBA.
I apologize if there are any errors in English, is not my natural language.
I've downloaded the file. Then change the Code to:
Public oldFormat As String
Private Sub ComboBox1_Change()
Dim ws As Worksheet
Dim newFormat As String
'On Error Resume Next
newValue = Me.ComboBox1.Value
Select Case newValue
Case "EUR"
newFormat = "#,##0 $"
Case "GBP"
newFormat = "[$£-809]#,##0"
Case "USD"
newFormat = "#,##0 [$USD]"
End Select
'Set rNextCell = Application.FindFormat
For Each ws In ActiveWorkbook.Worksheets
ws.Range("XFD1048576").NumberFormat = oldFormat
ws.Range("XFD1048576").NumberFormat = newFormat
ws.Range("XFD1048576").NumberFormat = "General"
Application.FindFormat.Clear
Application.FindFormat.NumberFormat = oldFormat
Application.ReplaceFormat.Clear
Application.ReplaceFormat.NumberFormat = newFormat
ws.Cells.Replace What:="", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=True, ReplaceFormat:=True
Next ws
End Sub
Private Sub ComboBox1_DropButtonClick()
inicial = Me.ComboBox1.Value
Select Case inicial
Case "EUR"
oldFormat = "#,##0 $"
Case "GBP"
oldFormat = "[$£-809]#,##0"
Case "USD"
oldFormat = "#,##0 [$USD]"
End Select
End Sub
Now it runs without errors.
But one issue is there yet. If the user defined format equals the default currency format, then Excel (Excel! not VBA) will not set the user defined format but the default currency format. This is for Germany in VBA "#,##0 $". With me this was the case with #.##0 € which is one of the German currency formats. So for me is EUR "#,##0 $" in VBA.
Greetings
Axel
Related
I'm using visual basic to create a checkout system in an excel sheet. The sheet will be filled with information for a project, each of the projects requires that we send out a kit. This excel sheet will allow for a barcode to be scanned, when this happens, it checks for puts an "out" time. When that barcode is scanned again it puts an "in" time. The issue I'm having is that if that barcode is scanned a third time, it will only update the out time.
How do I set it up where it will see that an "in" and "out" time have been recorded and thus go the next blank cell in the row and add the barcode + new "in" or "out" time. Any help would be greatly appreciated!
This is the code I am using.
Code for on the worksheet
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("B2")) Is Nothing Then
Application.EnableEvents = False
Call inout
Application.EnableEvents = True
End If
End Sub
code for the macro
Sub inout()
Dim barcode As String
Dim rng As Range
Dim rownumber As Long
barcode = Worksheets("Sheet1").Cells(2, 2)
Set rng = Sheet1.Columns("a:a").Find(What:=barcode, _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If rng Is Nothing Then
ActiveSheet.Columns("a:a").Find("").Select
ActiveCell.Value = barcode
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = Date & " " & Time
ActiveCell.NumberFormat = "m/d/yyyy h:mm AM/PM"
Worksheets("Sheet1").Cells(2, 2) = ""
Else
rownumber = rng.Row
Worksheets("Sheet1").Cells(rownumber, 1).Select
ActiveCell.Offset(0, 2).Select
ActiveCell.Value = Date & " " & Time
ActiveCell.NumberFormat = "m/d/yyyy h:mm AM/PM"
Worksheets("Sheet1").Cells(2, 2) = ""
End If
Worksheets("Sheet1").Cells(2, 2).Select
End Sub
All this goes in the worksheet code module:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("B2")) Is Nothing Then
inout 'use of Call is deprecated
End If
End Sub
Sub inout()
Dim barcode As String
Dim rng As Range
Dim newRow As Boolean
barcode = Me.Cells(2, 2)
'find the *last* instance of `barcode` in ColA
Set rng = Me.Columns("A").Find(What:=barcode, after:=Me.Range("A1"), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False, SearchFormat:=False)
'figure out if we need to add a new row, or update an existing one
If rng Is Nothing Then
newRow = True 'no matching barcode
Else
'does the last match already have an "in" timestamp?
If Len(rng.Offset(0, 2).Value) > 0 Then newRow = True
End If
If newRow Then
Set rng = Me.Cells(Me.Rows.Count, "A").End(xlUp).Offset(1, 0)
rng.Value = barcode
SetTime rng.Offset(0, 1) 'new row, so set "out"
Else
SetTime rng.Offset(0, 2) 'existing row so set "in"
End If
Me.Cells(2, 2).Select
End Sub
'set cell numberformat and set value to current time
Sub SetTime(c As Range)
With c
.NumberFormat = "m/d/yyyy h:mm AM/PM"
.Value = Now
End With
End Sub
I am receiving an Error 91 "Object variable or With block variable not set" when using .Find(What:-.
I want to find the column index number in the "overview" sheet by seaching for the value in Cells(2,2) from the "dailysheet".
I get the error on lnCol = line. I think it something to do with the formatting or setting the "checkdate" variable.
Any help would be great appreciated!
Sub checkingdate_Click()
Dim overview As Worksheet
Dim dailysheet As Worksheet
Dim datecheck As Range
Dim checkdate As Date
Dim lnRow As Long
Dim lnCol As Long
Set overview = ThisWorkbook.Worksheets("overview")
Set dailysheet = ThisWorkbook.Worksheets("dailysheet")
Set datecheck = dailysheet.Cells(2, 2)
lnRow = 5
overview.Rows("5").EntireRow.Hidden = False 'Adjust potentially
With datecheck
.NumberFormat = "dd/mm/yyyy"
'.NumberFormat = "#"
End With
With overview.Rows("5")
.NumberFormat = "dd/mm/yyyy"
'.NumberFormat = "#"
End With
checkdate = dailysheet.Cells(2, 2).Value
MsgBox datecheck.Value
MsgBox checkdate
lnCol = overview.Cells(lnRow, 1).EntireRow.Find(What:=checkdate, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
MsgBox lnCol
'=====
' END
'=====
overview.Rows("5").EntireRow.Hidden = True 'Adjust potentially
With overview.Rows("5")
.NumberFormat = "dd"
End With
With overview.Columns("B:ABO")
.ColumnWidth = 4.57
End With
End Sub
EDIT: Found a solution by changing the variable checkdate to dailysheet.Cells(2,2).Formula rather than .Values and changed to LookIn:=xlFormulas rather than LookIn:=xlValues. I also changed the .NumberFormat of overview.Rows("5"), so that the dates became serial numbers, thereby becoming searchable from the serial number from "checkdate".
Edited portion of the code is below:
With overview.Rows("5")
'.NumberFormat = "dd/mm/yyyy"
.NumberFormat = "#"
End With
checkdate = dailysheet.Cells(2, 2).Formula
MsgBox datecheck.Value
MsgBox checkdate
lnCol = overview.Cells(lnRow, 1).EntireRow.Find(What:=checkdate, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
Found a solution by changing the variable checkdate to dailysheet.Cells(2,2).Formula rather than .Values and changed to LookIn:=xlFormulas rather than LookIn:=xlValues. I also changed the .NumberFormat of overview.Rows("5"), so that the dates became serial numbers, thereby becoming searchable from the serial number from "checkdate".
Edited portion of the code is below:
With overview.Rows("5")
'.NumberFormat = "dd/mm/yyyy"
.NumberFormat = "#"
End With
checkdate = dailysheet.Cells(2, 2).Formula
MsgBox datecheck.Value
MsgBox checkdate
lnCol = overview.Cells(lnRow, 1).EntireRow.Find(What:=checkdate, LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
I am newbie to Excel VBA.
Need help to removing the commas, spaces and NULL string to "0" from ColumnName called StringName.
First, I have tried to remove the commas, spaces from the ColumnName called StringName and finally to find and replace the "NULL" string to 0(Zero).
Here is the code to Replace for commas, spaces from the ColumnName called StringName.
Sub ReplaceCharacters()
Application.ScreenUpdating = False
Dim lrow As Long
lrow = Cells(Cells.Rows.Count, "A").End(xlUp).Row
With ActiveSheet.Range("A2:A" & lrow).SpecialCells(xlCellTypeConstants, xlTextValues).Cells
.Replace What:=",", Replacement:="", LookAt:=xlPart
.Replace What:=" ", Replacement:="", LookAt:=xlPart
End With
Application.ScreenUpdating = True
End Sub
I am struggling to find and Replace the string "NULL" to 0 from the ColumnName called StringName.
I need help with this and I have tried a lot and ended up here for a solution.
Here is what I have tried..
' not working
Sub UpdateWhole()
With ActiveSheet.UsedRange
.Replace "NULL", "0", xlWhole
End With
End Sub
' not working
Sub FormulaRng()
For i = 2 To 10
Worksheets("Sheet1").Range("A2:A" & i).FormulaR1C1 = "=IF(A" & i & "=""NULL"",0,A" & i & ")"
Next
End Sub
Thanks in Advance
Here is the sample data to test
StringName
------------------
NULL
NULL
null
nullasdf
cbgrgNULLdf343
, asdfwe 4fdt
456fg , d55nullNULL
sdf34 df, 4fd
NULLfgf null
121
22
34545
Required OutPut
------------------------
0
0
0
nullasdf
cbgrgNULLdf343
asdfwe4fdt
456fgd55nullNULL
sdf34df4fd
NULLfgfnull
121
22
34545
The following revised procedure should work for you. I used the Clean function to remove all non-printable characters from the text before replacing the NULL text with 0.
VBA Code
Public Sub ReplaceCharacters()
On Error GoTo ErrTrap
Const ProcedureName As String = "ReplaceCharacters"
Dim sheet As Worksheet: Set sheet = ActiveSheet
Dim cell As Range
Application.ScreenUpdating = False
For Each cell In sheet.UsedRange.Cells
cell = Trim(Application.WorksheetFunction.Clean(cell))
cell.Replace What:="NULL", Replacement:=0, LookAt:=xlWhole
cell.Replace What:=",", Replacement:="", LookAt:=xlPart
cell.Replace What:=" ", Replacement:="", LookAt:=xlPart
Next cell
ExitProcedure:
On Error Resume Next
Application.ScreenUpdating = True
Set sheet = Nothing
Exit Sub
ErrTrap:
Select Case Err.number
Case Else
Debug.Print "Procedure: " & ProcedureName & " |Error #: " & Err.number & " |Error Description: " & Err.description
End Select
Resume ExitProcedure
Resume 'for debugging
End Sub
Example Video
Try this.
Sub test()
Dim Ws As Worksheet
Dim rngDB As Range
Set Ws = ActiveSheet
With Ws
Set rngDB = .Range("a2", .Range("a" & Rows.Count).End(xlUp))
End With
With rngDB
.Replace What:=",", Replacement:="", LookAt:=xlPart
.Replace What:=" ", Replacement:="", LookAt:=xlPart
.Replace What:="NULL", Replacement:="0", LookAt:=xlWhole
.Replace What:="null", Replacement:="0", LookAt:=xlWhole
End With
End Sub
I need your help,
I can't seem to get the next or previous buttons to work with the .FindNext and FindPrevious functions of excel.
My aim is to create a user form where the user can use the next and prev buttons to go back and fourth between the found matches of "test". I thought that by globalizing the variable foundCell, I might be able to accomplish this, but I was epically wrong.
Dim foundCell
Private Sub btnSearch_Click()
With Sheet1
Set foundCell = .Cells.find(What:="test", After:=.Cells(1, 1), _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
End With
If Not foundCell Is Nothing Then
MsgBox ("""Bingo"" found in row " & foundCell.Row)
form1.location.Value = Cells(foundCell.Row, 1).Value
Else
MsgBox ("Bingo not found")
End If
End Sub
Private Sub btnNext_Click()
foundCell.FindNext
form1.location.Value = Cells(foundCell.Row, 1).Value
End Sub
Private Sub btnPrev_Click()
foundCell.FindPrevious
form1.location.Value = Cells(foundCell.Row, 1).Value
End Sub
I would take your search routine and move it into a sub routine. Then you can just call it by passing in a few params. like the starting cell to search from and which direction to go.
Private Sub btnSearch_Click()
dosearch Cells(1, 1), Excel.xlNext
End Sub
Private Sub btnNext_Click()
dosearch foundCell, Excel.xlNext
End Sub
Private Sub btnPrev_Click()
dosearch foundCell, Excel.xlPrevious
End Sub
Sub dosearch(r As Range, whichWay As Integer)
With Sheet1
Set foundCell = .Cells.Find(What:="test", After:=r, _
LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=whichWay, MatchCase:=False, SearchFormat:=False)
End With
If Not foundCell Is Nothing Then
MsgBox ("""Bingo"" found in row " & foundCell.Row)
form1.Location.Value = Cells(foundCell.Row, 1).Value
Else
MsgBox ("Bingo not found")
End If
End Function
My vba code below, how do it faster ? (obs: i have +- 33000 lines of values)
I search codes from products to my company, i need help to do it faster.
Private Sub TextBox1_Enter()
Dim FindString As String
Dim Rng As Range
FindString = TextBox1.Text
If Trim(FindString) <> "" And Len(TextBox1.Text) = 6 Then
With Sheets("CADMAT").Range("B:B") 'searches all of column B
Set Rng = .Find(What:=FindString, _
After:=.Cells(.Cells.Count), _
LookIn:=xlValues, _
LookAt:=xlWhole, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
If Not Rng Is Nothing Then
Dim ultimalinha As Object
Set ultimalinha = Plan3.Range("A35565").End(xlUp)
ultimalinha.Offset(1, 0).Value = TextBox1.Text
ultimalinha.Offset(1, 1).Value = TextBox2.Text
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.SetFocus
Else
MsgBox "Produto não existe na tabela!" 'value not found
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.SetFocus
End If
End With
End If
End Sub
Option Explicit
Private Sub TextBox1_Enter()
Application.ScreenUpdating = False
Code here ...
Application.ScreenUpdating = True
End Sub