Skip to content

Commit 085d2d2

Browse files
added "Multivalue cells" macro
1 parent 6bfa76b commit 085d2d2

File tree

6 files changed

+262
-0
lines changed

6 files changed

+262
-0
lines changed

roadmap/excel-helpers.xmind

13.8 KB
Binary file not shown.
73.1 KB
Binary file not shown.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
VERSION 5.00
2+
Begin {C62A69F0-16DC-11CE-9E98-00AA00574A4F} FMultiValues
3+
Caption = "Select Values for Cell:"
4+
ClientHeight = 3555
5+
ClientLeft = 45
6+
ClientTop = 375
7+
ClientWidth = 7200
8+
OleObjectBlob = "FMultiValues.frx":0000
9+
StartUpPosition = 1 'CenterOwner
10+
End
11+
Attribute VB_Name = "FMultiValues"
12+
Attribute VB_GlobalNameSpace = False
13+
Attribute VB_Creatable = False
14+
Attribute VB_PredeclaredId = True
15+
Attribute VB_Exposed = False
16+
Private Sub btnAll_Click()
17+
Dim i As Integer
18+
19+
For i = 0 To lstVal.ListCount - 1
20+
lstVal.Selected(i) = True
21+
Next
22+
End Sub
23+
24+
Private Sub btnCancel_Click()
25+
Me.Hide
26+
End Sub
27+
28+
Private Sub btnOK_Click()
29+
30+
Dim selectedValues As String
31+
Dim i As Integer
32+
Dim s As Variant
33+
34+
selectedValues = vbNullString
35+
36+
For i = 0 To lstVal.ListCount - 1
37+
If lstVal.Selected(i) = True Then selectedValues = selectedValues & lstVal.List(i) & txtSep.text
38+
Next
39+
40+
For Each s In Selection.cells
41+
42+
If selectedValues = vbNullString Then
43+
s.Value = vbNullString
44+
Else
45+
s.Value = Mid(selectedValues, 1, Len(selectedValues) - Len(txtSep.text))
46+
End If
47+
48+
Next
49+
50+
Me.Hide
51+
52+
End Sub
53+
54+
Private Sub btnReset_Click()
55+
Dim i As Integer
56+
57+
For i = 0 To lstVal.ListCount - 1
58+
lstVal.Selected(i) = False
59+
Next
60+
61+
End Sub
62+
63+
Private Sub btnUpdateFromCells_Click()
64+
Dim cell As Variant
65+
66+
On Error Resume Next
67+
68+
lstVal.Clear
69+
70+
For Each cell In Selection.cells
71+
lstVal.AddItem cell.Value
72+
Next
73+
74+
Me.Hide
75+
End Sub
76+
77+
Private Sub btnUpdateOptions_Click()
78+
Dim varArray As Variant
79+
Dim cell As Variant
80+
81+
On Error Resume Next
82+
83+
varArray = Split(PasteFromClipboard(), vbCrLf)
84+
85+
lstVal.Clear
86+
87+
For Each cell In varArray
88+
If cell <> vbNullString Then lstVal.AddItem cell
89+
Next
90+
91+
End Sub
3.52 KB
Binary file not shown.
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
Attribute VB_Name = "MClipboard"
2+
Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
3+
Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
4+
Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long
5+
Declare Function CloseClipboard Lib "User32" () As Long
6+
Declare Function OpenClipboard Lib "User32" (ByVal hwnd As Long) As Long
7+
Declare Function EmptyClipboard Lib "User32" () As Long
8+
Declare Function lstrcpy Lib "kernel32" (ByVal lpString1 As Any, ByVal lpString2 As Any) As Long
9+
Declare Function SetClipboardData Lib "User32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
10+
Declare Function GetClipboardData Lib "User32" (ByVal wFormat As Long) As Long
11+
Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
12+
13+
Public Const GHND = &H42
14+
Public Const CF_TEXT = 1
15+
Public Const MAXSIZE = 4096
16+
17+
Private Const RAISE_ERROR = False
18+
19+
Public Function CopyToClipboard(myString As String) As Boolean
20+
Dim hGlobalMemory As Long, lpGlobalMemory As Long
21+
Dim hClipMemory As Long, hTmp As Long
22+
23+
' Allocate moveable global memory.
24+
hGlobalMemory = GlobalAlloc(GHND, Len(myString) + 1)
25+
26+
' Lock the block to get a far pointer to this memory.
27+
lpGlobalMemory = GlobalLock(hGlobalMemory)
28+
29+
' Copy the string to this global memory.
30+
lpGlobalMemory = lstrcpy(lpGlobalMemory, myString)
31+
32+
' Unlock the memory.
33+
If GlobalUnlock(hGlobalMemory) <> 0 Then
34+
If RAISE_ERROR Then
35+
Err.Raise 520, "CopyToClipboard", "Could not unlock memory location."
36+
Else
37+
CopyToClipboard = False
38+
GoTo mrk
39+
End If
40+
End If
41+
42+
' Open the Clipboard to copy data to.
43+
If OpenClipboard(0&) = 0 Then
44+
If RAISE_ERROR Then
45+
Err.Raise 521, "CopyToClipboard", "Could not open the Clipboard."
46+
Else
47+
CopyToClipboard = False
48+
Exit Function
49+
End If
50+
End If
51+
52+
' Clear the Clipboard.
53+
hTmp = EmptyClipboard()
54+
55+
' Copy the data to the Clipboard.
56+
hClipMemory = SetClipboardData(CF_TEXT, hGlobalMemory)
57+
58+
mrk:
59+
60+
If CloseClipboard() = 0 Then
61+
If RAISE_ERROR Then
62+
Err.Raise 521, "CopyToClipboard", "Could not close the Clipboard."
63+
Else
64+
CopyToClipboard = False
65+
Exit Function
66+
End If
67+
Else
68+
CopyToClipboard = True
69+
End If
70+
71+
End Function
72+
73+
Public Function PasteFromClipboard() As String
74+
Dim hClipMemory As Long
75+
Dim lpClipMemory As Long
76+
Dim myString As String
77+
Dim RetVal As Long
78+
79+
If OpenClipboard(0&) = 0 Then
80+
If RAISE_ERROR Then
81+
Err.Raise 521, "PasteFromClipboard", "Could not open the Clipboard."
82+
Else
83+
PasteFromClipboard = vbNullString
84+
Exit Function
85+
End If
86+
End If
87+
88+
' Obtain the handle to the global memory block that is referencing the text.
89+
hClipMemory = GetClipboardData(CF_TEXT)
90+
91+
If IsNull(hClipMemory) Then
92+
If RAISE_ERROR Then
93+
Err.Raise 520, "PasteFromClipboard", "Could not allocate memory."
94+
Else
95+
PasteFromClipboard = False
96+
GoTo mrk
97+
End If
98+
End If
99+
100+
' Lock Clipboard memory so we can reference the actual data string.
101+
lpClipMemory = GlobalLock(hClipMemory)
102+
103+
If Not IsNull(lpClipMemory) Then
104+
myString = Space$(MAXSIZE)
105+
RetVal = lstrcpy(myString, lpClipMemory)
106+
RetVal = GlobalUnlock(hClipMemory)
107+
108+
' Peel off the null terminating character.
109+
myString = Mid(myString, 1, InStr(1, myString, Chr$(0), 0) - 1)
110+
Else
111+
If RAISE_ERROR Then
112+
Err.Raise 520, "PasteFromClipboard", "Could not lock memory to copy string from."
113+
Else
114+
PasteFromClipboard = False
115+
GoTo mrk
116+
End If
117+
End If
118+
119+
mrk:
120+
121+
RetVal = CloseClipboard()
122+
PasteFromClipboard = myString
123+
124+
End Function
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Attribute VB_Name = "MMultivalues"
2+
Private frm As FMultiValues
3+
Private isLoaded As Boolean
4+
5+
Public Sub ShowMultiValuesBox()
6+
Dim sep As String
7+
Dim old As Variant
8+
Dim o As Variant
9+
10+
If isLoaded Then
11+
If frm.lstVal.ListCount > 0 Then
12+
For i = 0 To frm.lstVal.ListCount - 1
13+
frm.lstVal.Selected(i) = False
14+
Next
15+
16+
sep = frm.txtSep.text
17+
old = Split(ActiveCell.Value, sep)
18+
19+
For Each o In old
20+
For i = 0 To frm.lstVal.ListCount - 1
21+
If frm.lstVal.List(i) = o Then
22+
frm.lstVal.Selected(i) = True
23+
Exit For
24+
End If
25+
Next
26+
Next
27+
End If
28+
Else
29+
Set frm = New FMultiValues
30+
isLoaded = True
31+
End If
32+
33+
If Selection.cells.Count > 1 Then
34+
o = Replace(Selection.Address, "$", "")
35+
frm.Caption = "Select values for cells {" & o & "}"
36+
Else
37+
o = Replace(ActiveCell.Address, "$", "")
38+
39+
sep = ActiveCell.Value
40+
If sep = vbNullString Then sep = "empty"
41+
42+
frm.Caption = "Select values for cell {" & o & "} current value: " & sep
43+
End If
44+
45+
frm.lstVal.SetFocus
46+
frm.Show 1
47+
End Sub

0 commit comments

Comments
 (0)