Here is a question i have for importing data from Access to Excel.
So first I need to write data from Excel to Access (when the data is saved in Access, the data are saved as the access units by doing a unit conversion of the data from Excel).
After that, I may want to recall these data back into excel and the excel needs to convert the recalled data back into the base units of Excel.
That being said, for cells with money values, I assign these cells with a unit type of CURRENCY
. When these data is saved in Access, they become $. But when they are recalled back into Excel, the base unit in excel is M$. I need to figure out a coding that let these cells value divided by 1000 so they become M$ in Excel. After that, I will have a droplist that let me convert these values to either $ or MM$ when i want to. The conversions for bbl and mcf are already done by original user.
Not sure if anyone can give me a easy solution for this...I have the code below:
Set rs2 = db.OpenRecordset("SELECT * FROM Project_Data WHERE LoadID = " & _
record_ID & " ORDER BY LoadID Asc", dbReadOnly)
For v = 1 To 1244
v_name = vars(v, 1)
If vars(v, 2) = "Y" Then
rs2.MoveFirst
Do
If rs2![VariableName] = v_name Then ' And rs2![LoadID] = record_ID Then
If Mid(v_name, 1, 2) = "T1" Then
Sheets("T1").Range(v_name) = Val(rs2![VariableValue])
End If
If Mid(v_name, 1, 2) = "T2" Then
Sheets("T2").Range(v_name) = Val(rs2![VariableValue])
End If
If Mid(v_name, 1, 2) = "T3" Then
Sheets("T3").Range(v_name) = Val(rs2![VariableValue])
End If
If Mid(v_name, 1, 2) = "T4" Then
Sheets("T4").Range(v_name) = Val(rs2![VariableValue])
End If
If Mid(v_name, 1, 2) = "T5" Then
Sheets("T5").Range(v_name) = Val(rs2![VariableValue])
End If
If Mid(v_name, 1, 2) = "T6" Then
Sheets("T6").Range(v_name) = Val(rs2![VariableValue])
End If
If Mid(v_name, 1, 2) = "T7" Then
Sheets("T7").Range(v_name) = Val(rs2![VariableValue])
End If
Exit Do
End If
rs2.MoveNext
Loop Until rs2.EOF
End If
Next v
rs2.Close
Set rs2 = Nothing
db.Close
Set db = Nothing
Unload Me
write_log ("Imported existing record.")
Sheets("Main").Range("GASUNITS") = "MMcf"
ThisWorkbook.gas_units = "MMcf"
Sheets("Main").Range("LIQUIDUNITS") = "Mbbl"
ThisWorkbook.liquid_units = "Mbbl"
Call Switch_Liquids_Units("Mbbl")
Call Switch_Gas_Units("MMcf")
MsgBox ("Data has been loaded.")
End Sub
Coding for switch_Liquids_Units:
Sub Switch_Liquids_Units(units)
If Range("LIQUIDUNITS") = "Mbbl" Then
pass_unit = "M"
pass_fluid = "Oil"
End If
If Range("LIQUIDUNITS") = "MMbbl" Then
pass_unit = "MM"
pass_fluid = "Oil"
End If
If Range("LIQUIDUNITS") = "bbl" Then
pass_unit = ""
pass_fluid = "Oil"
End If
Call Replace_Units(pass_unit, pass_fluid)
Sheets("Main").Select
If do_old = True Then
Sheets("T1").Select
'Rows("3:3").Select
Range("B3:AE3").Select
If units = "bbl" Then
Selection.Replace What:="(Mbbl)", Replacement:="(bbl)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="(MMbbl)", Replacement:="(bbl)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="(Mboe)", Replacement:="(boe)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="(MMboe)", Replacement:="(boe)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End If
If units = "Mbbl" Then
Range("B3:AE3").Replace What:="(bbl)", Replacement:="(Mbbl)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="(MMbbl)", Replacement:="(Mbbl)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="(boe)", Replacement:="(Mboe)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="(MMboe)", Replacement:="(Mboe)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End If
If units = "MMbbl" Then
Selection.Replace What:="(bbl)", Replacement:="(MMbbl)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="(Mbbl)", Replacement:="(MMbbl)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="(boe)", Replacement:="(MMboe)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="(Mboe)", Replacement:="(MMboe)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End If
Sheets("T2").Select
'Rows("4:4").Select
Range("B4:AN4").Select
If units = "bbl" Then
Selection.Replace What:="(Mbbl)", Replacement:="(bbl)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="(MMbbl)", Replacement:="(bbl)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End If
If units = "Mbbl" Then
Selection.Replace What:="(bbl)", Replacement:="(Mbbl)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="(MMbbl)", Replacement:="(Mbbl)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End If
If units = "MMbbl" Then
Selection.Replace What:="(bbl)", Replacement:="(MMbbl)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:="(Mbbl)", Replacement:="(MMbbl)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End If
End If
End Sub