When I train my Autoencoder I always get the same output - tensorflow

Im creatting an autoencoder to learn features from markets images, but when I train the model I always get the same output. Could anyone help to me?
This is the code:
from keras import layers
input_img = keras.Input(shape=(224, 224, 3))
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same',activity_regularizer=regularizers.L2(1e-4))(input_img)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same',activity_regularizer=regularizers.L2(1e-4))(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(256, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = layers.MaxPooling2D((2, 2), padding='same')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same')(x)
encoded = layers.AveragePooling2D((14, 14), padding='same')(x)
x = layers.Conv2D(784, (3, 3), activation='relu', padding='same')(encoded)
x = layers.Conv2D(784, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(784, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((7, 7))(x)
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(16, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same')(x)
x = layers.UpSampling2D((2, 2))(x)
decoded = layers.Conv2D(3, (3, 3), activation='softmax', padding='same')(x)
autoencoder=keras.Model(input_img, decoded)
autoencoder.compile(optimizer='adamax', loss='mse',metrics=["accuracy"])
autoencoder.summary()
I dont know how to deal with this problem, Im new in this kind of task.

Related

I am getting error layer max_pooling2d_111 is incompatible with the layer: expected ndim=4, found ndim=5

I am currently writing the code, the model to take 4 images as an input. All 4 images are passed through the convolution network and then all image's output is concatenated. But while compiling I am getting an error of "Input 0 of layer max_pooling2d_111 is incompatible with the layer: expected ndim=4 found ndim=5. Full shape received: [None, 4, 128, 128, 16]"
I know there is 1 more dimension in the input (size of 4) but how should I do this or solve this issue?
My model code is :
#Build the model
inputs1 = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
o1 = tf.keras.layers.Lambda(lambda x: x / 255)(inputs1)
c1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(o1)
c1 = tf.keras.layers.Dropout(0.1)(c1)
c1 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)
p1 = tf.keras.layers.MaxPooling2D((2, 2))(c1)
#p1 = c1
c1 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p1)
c1 = tf.keras.layers.Dropout(0.1)(c1)
c1 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)
p1 = tf.keras.layers.MaxPooling2D((2, 2))(c1)
#p1 = c1
c1 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p1)
c1 = tf.keras.layers.Dropout(0.2)(c1)
c1 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)
p1 = tf.keras.layers.MaxPooling2D((2, 2))(c1)
#p1 = c1
c1 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p1)
c1 = tf.keras.layers.Dropout(0.2)(c1)
c1 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c1)
p1 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(c1)
#p1 = c1
inputs2 = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
o2 = tf.keras.layers.Lambda(lambda x: x / 255)(inputs2)
c2 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(o2)
c2 = tf.keras.layers.Dropout(0.1)(c2)
c2 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)
p2 = tf.keras.layers.MaxPooling2D((2, 2))(c2)
#p2 = c2
c2 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p2)
c2 = tf.keras.layers.Dropout(0.1)(c2)
c2 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)
p2 = tf.keras.layers.MaxPooling2D((2, 2))(c2)
#p2 = c2
c2 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p2)
c2 = tf.keras.layers.Dropout(0.2)(c2)
c2 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)
p2 = tf.keras.layers.MaxPooling2D((2, 2))(c2)
#p2 = c2
c2 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p2)
c2 = tf.keras.layers.Dropout(0.2)(c2)
c2 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c2)
p2 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(c2)
#p2 = c2
inputs3 = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
o3 = tf.keras.layers.Lambda(lambda x: x / 255)(inputs3)
c3 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(o3)
c3 = tf.keras.layers.Dropout(0.1)(c3)
c3 = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)
p3 = tf.keras.layers.MaxPooling2D((2, 2))(c3)
#p3 = c3
c3 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p3)
c3 = tf.keras.layers.Dropout(0.1)(c3)
c3 = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)
p3 = tf.keras.layers.MaxPooling2D((2, 2))(c3)
#p3 = c3
c3 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p3)
c3 = tf.keras.layers.Dropout(0.2)(c3)
c3 = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)
p3 = tf.keras.layers.MaxPooling2D((2, 2))(c3)
#p3 = c3
c3 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p3)
c3 = tf.keras.layers.Dropout(0.2)(c3)
c3 = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c3)
p3 = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(c3)
#p3 = c3
P = tf.keras.layers.concatenate([p1, p2, p3])
cp = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(P)
cp = tf.keras.layers.Dropout(0.3)(cp)
cp = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(cp)
#Build the model
inputs = tf.keras.layers.Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
s = tf.keras.layers.Lambda(lambda x: x / 255)(inputs)
c = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(s)
c = tf.keras.layers.Dropout(0.1)(c)
c = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c)
p = tf.keras.layers.MaxPooling2D((2, 2))(c)
#p = c
c = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p)
c = tf.keras.layers.Dropout(0.1)(c)
c = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c)
p = tf.keras.layers.MaxPooling2D((2, 2))(c)
#p = c
c = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p)
c = tf.keras.layers.Dropout(0.2)(c)
c = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c)
p = tf.keras.layers.MaxPooling2D((2, 2))(c)
#p = c
c = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p)
c = tf.keras.layers.Dropout(0.2)(c)
c = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c)
p = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(c)
#p = c
c = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(p)
c = tf.keras.layers.Dropout(0.3)(c)
c = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(c)
one = tf.keras.layers.concatenate([cp,c])
#one = tf.keras.layers.add([cp,c])
#Compression
onec = tf.keras.layers.Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(one)
onec = tf.keras.layers.Dropout(0.3)(onec)
onec = tf.keras.layers.Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(onec)
onep = tf.keras.layers.MaxPooling2D(pool_size=(2, 2))(onec)
#Decompression
d = tf.keras.layers.Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(onep)
d = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Dropout(0.3)(d)
d = tf.keras.layers.Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(d)
d = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Dropout(0.3)(d)
d = tf.keras.layers.Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(d)
d = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Dropout(0.2)(d)
d = tf.keras.layers.Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Conv2DTranspose(332, (2, 2), strides=(2, 2), padding='same')(d)
d = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Dropout(0.1)(d)
d = tf.keras.layers.Conv2D(32, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same')(d)
d = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
d = tf.keras.layers.Dropout(0.1)(d)
d = tf.keras.layers.Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same')(d)
outputs = tf.keras.layers.Conv2D(1, (1, 1), activation='relu',name = "ouput")(d)
model = tf.keras.Model(inputs=[inputs,inputs1,inputs2,inputs3], outputs=[outputs])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
Anyone can please help me or suggest to me how to do it.

Modify select query with multi condition cases

I have the following table dbo.Product:
Name Amount
----------
A 10
B 4
C 6
D 3
A 7
I have condition: if name is equal to 'A', then show code 'X' and discount '10%', otherwise show code 'Y' and discount '5%'
I want to modify the following query, could you advice the better solution :
select
case p.Name
when 'A' then 'X' else 'Y' end as Code,
case p.Name
when 'A' then (p.Amount * 0.1) else (p.Amount * 0.05) end as Discount,
p.Name,
p.Amount
from
dbo.Product as P
There are other ways of writing this query - but using a case expression in the select clause seems to be the best option.
I've taken your sample data and multiplied it to 840 rows so that I can get some insight on performance. Then I've tested three different approaches to get the desired results:
First, the query from the question - it had the best performance of all three.
Second, the union all query suggested by Gauravsa, and third, a cross apply version.
Results (on my server) are these:
Case expressions: 16%
Union all: 36%
Cross Apply: 48%
You can clearly see from the results that the case expressions version is by far the winner - Mainly because of the fact that computing scalars is a very cheap operation compared to table or index scans and even index seek.
Please note that I've tested on a temporary table with no indexing whatsoever so a test with real live data might yield different results - however I don't imagine the order of the results to change - I mean, the case expression version will probably still win.
Than being said, Starting from 2012 version, there is a shorter way to write simple case expressions: The IFF function.
select
IIF(p.Name = 'A', 'X', 'Y') as Code,
IIF(p.Name = 'A', 0.1, 0.05) * p.Amount as Discount,
p.Name,
p.Amount
from
#Product as P
This query is an exact equivalent to the query from the question - just with slightly less code.
Here's the full script from my tests so that anyone can repeat it or find problems in the way I've tested: (the result execution plan can be found here)
-- Create sample data
create table #Product
(
Name char(1),
Amount int
);
insert into #Product (Name, Amount) values
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),
('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7),('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7), ('A', 10), ('B', 4), ('C', 6), ('D', 3), ('A', 7);
-- Cross apply version: nice trick, too slow.
select
Code,
Discount,
p.Name,
p.Amount
from
#Product as P
cross apply
(
select 'X' as code, p.Amount * 0.1 As Discount
where p.Name = 'A'
union all
select 'Y', p.Amount * 0.5
where p.Name <> 'A'
) x
-- Case expression: OP's query
select
case p.Name
when 'A' then 'X' else 'Y' end as Code,
case p.Name
when 'A' then (p.Amount * 0.1) else (p.Amount * 0.05) end as Discount,
p.Name,
p.Amount
from
#Product as P
-- Union all - Gauravsa's query
select
p.Name,
p.Amount,
'X' as Code,
p.Amount * 0.1 as Discount
from
#Product as P
where p.Name = 'A'
union all
select
p.Name,
p.Amount,
'Y' as Code,
p.Amount * 0.05 as Discount
from
#Product as P
where p.Name != 'A'
-- Clean up
drop table #Product
One way is:
Select
p.Name,
p.Amount,
'X' as Code,
p.Amount * 0.1 as Discount
from
dbo.Product as P
where p.Name = 'A'
Union All
Select
p.Name,
p.Amount,
'Y' as Code,
p.Amount * 0.05 as Discount
from
dbo.Product as P
where p.Name != 'A';
!= in sql server:
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/not-equal-to-transact-sql-exclamation?view=sql-server-2017
DB Fiddle:
Select ##version;
Create Table Product (Name varchar(1), Amount money);
Insert into Product values ('A', 10);
Insert into Product values ('B', 4);
Insert into Product values ('C', 6);
Insert into Product values ('D', 3);
Insert into Product values ('A', 7);
Select
case p.Name
when 'A' then 'X' else 'Y' end as Code,
case p.Name
when 'A' then (p.Amount * 0.1) else (p.Amount * 0.05) end as Discount,
p.Name,
p.Amount
from
Product as P;
Select
'X' as Code,
p.Amount * 0.1 as Discount,
p.Name,
p.Amount
from
dbo.Product as P
where p.Name = 'A'
Union all
Select
'Y' as Code,
p.Amount * 0.05 as Discount,
p.Name,
p.Amount
from
dbo.Product as P
where p.Name != 'A';
Try this in a single query like below
Select
p.Name,
p.Amount,
CASE WHEN p.Name = 'A' THEN 'X' ELSE 'Y' END as Code,
CASE WHEN p.Name <> 'A' THEN (p.Amount * 0.1) ELSE (p.Amount * 0.05) END as Discount,
from dbo.Product as P

Excel Function to Access Function

I created UPC check-digit function in excel which is working perfectly fine but i am now in need of creating same thing in MS Access.
here is the code:
Public Function CHECKDIGIT(UPC As String) As String
If Len(UPC) = 8 Then
CHECKDIGIT = WorksheetFunction.RoundUp(WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1)), -1) - (WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1)))
ElseIf Len(UPC) = 12 Then
CHECKDIGIT = WorksheetFunction.RoundUp(WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1)), -1) - (WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1)))
ElseIf Len(UPC) = 13 Then
CHECKDIGIT = WorksheetFunction.RoundUp(WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1)) + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1)) * 3, -1) - (WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1)) + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1)) * 3)
ElseIf Len(UPC) = 14 Then
CHECKDIGIT = WorksheetFunction.RoundUp(WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1), Mid(UPC, 13, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1)), -1) - (WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1), Mid(UPC, 13, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1)))
ElseIf Len(UPC) = 17 Then
CHECKDIGIT = WorksheetFunction.RoundUp(WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1), Mid(UPC, 13, 1), Mid(UPC, 15, 1)) + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1), Mid(UPC, 14, 1), Mid(UPC, 16, 1)) * 3, -1) - (WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1), Mid(UPC, 13, 1), Mid(UPC, 15, 1)) + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1), Mid(UPC, 14, 1), Mid(UPC, 16, 1)) * 3)
ElseIf Len(UPC) = 18 Then
CHECKDIGIT = WorksheetFunction.RoundUp(WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1), Mid(UPC, 13, 1), Mid(UPC, 15, 1), Mid(UPC, 17, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1), Mid(UPC, 14, 1), Mid(UPC, 16, 1)), -1) - (WorksheetFunction.Sum(Mid(UPC, 1, 1), Mid(UPC, 3, 1), Mid(UPC, 5, 1), Mid(UPC, 7, 1), Mid(UPC, 9, 1), Mid(UPC, 11, 1), Mid(UPC, 13, 1), Mid(UPC, 15, 1), Mid(UPC, 17, 1)) * 3 + WorksheetFunction.Sum(Mid(UPC, 2, 1), Mid(UPC, 4, 1), Mid(UPC, 6, 1), Mid(UPC, 8, 1), Mid(UPC, 10, 1), Mid(UPC, 12, 1), Mid(UPC, 14, 1), Mid(UPC, 16, 1)))
ElseIf Len(UPC) <> 8 Or Len(UPC) <> 12 Or Len(UPC) <> 13 Or Len(UPC) <> 14 Or Len(UPC) <> 17 Or Len(UPC) <> 18 Then
CHECKDIGIT = "MISSING DIGITS"
End If
End Function
Is there any easy way to convert this to MS Access?
To my knowledge not all formulas will transfer directly to access so I image I will need to build custom function in Access too.
Any idea how to go with this?
Thanks,
Slav
The following code should work equally well in Excel and Access:
Public Function CHECKDIGIT(UPC As String) As String
Dim n As Integer
Dim i As Integer
Select Case Len(UPC)
Case 8, 12, 13, 14, 17, 18
n = 0
For i = 1 To Len(UPC) - 1
If ((Len(UPC) - i) Mod 2) = 1 Then
n = n + CInt(Mid(UPC, i, 1)) * 3
Else
n = n + CInt(Mid(UPC, i, 1))
End If
Next
CHECKDIGIT = CStr(Int(n / 10 + 0.99) * 10 - n)
Case Else
CHECKDIGIT = "MISSING DIGITS"
End Select
End Function
Thank you for the code YowE3K. I think there is something missing in that code. When i run it against results i get using my code It fails.
UPC CHCKDIGIT NEW CHKDIGIT
018371009355 5 5
00018371009355 5 5
7501206634004 4 10
00018371019354 4 14
018371019354 4 14
Also by using function i just found out that i cant use the function as part of data entry validation. I will have to use formula using what Comintern suggested above.
Thank you both guys for helping with this.

Visual basic matrices rotation

I made a square using 2 1 by 4 matrices, however now I want to rotate it by 45 degrees.
This is the code for the square (which works fine):
Dim m(1, 4) As Single
Dim n(1, 4) As Single
Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Black)
formGraphics.DrawLine(myPen, m(1, 1), m(1, 2), n(1, 4), n(1, 3))
formGraphics.DrawLine(myPen, n(1, 1), n(1, 3), m(1, 4), m(1, 3))
formGraphics.DrawLine(myPen, n(1, 2), n(1, 4), m(1, 2), m(1, 1))
formGraphics.DrawLine(myPen, n(1, 3), n(1, 3), m(1, 3), m(1, 1))
myPen.Dispose()
formGraphics.Dispose()
However when I try and turn it by 45 degrees I just cannot seem to get it right and I have no idea what I'm doing wrong or how to fix it. Code so far:
Dim b(1, 4) As Single
Dim c(1, 4) As Single
Dim A(2, 2) As Single
Dim radians As Double
Dim degrees As Double
degrees = 45
radians = degrees * ((2 * PI) / 360)
A(1, 1) = Cos(radians)
A(1, 2) = -Sin(radians)
A(2, 1) = Sin(radians)
A(2, 2) = Cos(radians)
c(1, 1) = (A(1, 1) * m(1, 2)) + (A(1, 2) * n(1, 2))
c(1, 2) = (A(1, 1) * m(1, 3)) + (A(2, 2) * n(1, 3))
c(1, 3) = (A(1, 1) * m(1, 3)) + (A(2, 2) * n(1, 3))
c(1, 4) = (A(1, 1) * m(1, 2)) + (A(1, 2) * n(1, 2))
b(1, 1) = (A(2, 1) * m(1, 2)) + (A(2, 2) * n(1, 2))
b(1, 2) = (A(2, 1) * m(1, 3)) + (A(2, 2) * n(1, 3))
b(1, 3) = (A(2, 1) * m(1, 3)) + (A(2, 2) * n(1, 3))
b(1, 4) = (A(2, 1) * m(1, 4)) + (A(2, 2) * n(1, 4))
Dim formGraphics As System.Drawing.Graphics = Me.CreateGraphics()
Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Black)
formGraphics.DrawLine(myPen, b(1, 3), b(1, 4), c(1, 4), c(1, 4))
formGraphics.DrawLine(myPen, c(1, 4), c(1, 2), b(1, 4), b(1, 4))
formGraphics.DrawLine(myPen, b(1, 1), c(1, 2), b(1, 4), b(1, 4))
formGraphics.DrawLine(myPen, c(1, 4), c(1, 1), b(1, 4), b(1, 2))
myPen.Dispose()
formGraphics.Dispose()

Sending excel objects to subroutines

As it is right now I have a bit of code that kind of looks like this (a little paraphrased but Im sure you get the idea)
If ComboBox1.SelectedIndex = 1 Then
swEV2.Stop()
If ComboBox3.SelectedIndex = 0 Then
xlWorkSheet202.Activate()
xlWorkSheet202.Cells((AT + 2), 3) = TextBox1.Text
xlWorkSheet202.Cells((AT + 3), 2) = "PSS (kBs)"
xlWorkSheet202.Cells((AT + 3), 3) = "USS (kBs)"
xlWorkSheet202.Cells((AT + 3), 4) = "User %"
xlWorkSheet202.Cells((AT + 3), 5) = "Kernel %"
xlWorkSheet202.Cells((AT + 3), 6) = "Total %"
xlWorkSheet202.Cells((AT + 4), 1) = "Min:"
xlWorkSheet202.Cells((AT + 5), 1) = "Max:"
xlWorkSheet202.Cells((AT + 6), 1) = "Average:"
xlWorkSheet202.Cells((AT + 7), 1) = "Median:"
xlWorkSheet202.Cells((AT + 8), 1) = "Stan Dev:"
ElseIf ComboBox3.SelectedIndex = 2 Then
xlWorkSheet204.Cells((WT + 2), 3) = TextBox1.Text
xlWorkSheet204.Cells((WT + 3), 2) = "PSS (kBs)"
xlWorkSheet204.Cells((WT + 3), 3) = "USS (kBs)"
xlWorkSheet204.Cells((WT + 3), 4) = "User %"
xlWorkSheet204.Cells((WT + 3), 5) = "Kernel %"
xlWorkSheet204.Cells((WT + 3), 6) = "Total %"
xlWorkSheet204.Cells((WT + 4), 1) = "Min:"
xlWorkSheet204.Cells((WT + 5), 1) = "Max:"
xlWorkSheet204.Cells((WT + 6), 1) = "Average:"
xlWorkSheet204.Cells((WT + 7), 1) = "Median:"
xlWorkSheet204.Cells((WT + 8), 1) = "Stan Dev:"
This goes on 3 more times in a few different places... So now I am trying to refactor the code to make it cleaner and shorter.
What I would like to do is this:
If ComboBox1.SelectedIndex = 1 Then
swEV2.Stop()
If ComboBox3.SelectedIndex = 0 Then
Excelupdate(xlWorkSheet203, AT)
ElseIf ComboBox3.SelectedIndex = 2 Then
Excelupdate(xlWorkSheet204, WT)
Private sub ExcelUpdate(byref worksheet as object, byref update as string)
worksheet.Activate()
worksheet.Cells((update + 2), 3) = TextBox1.Text
worksheet.Cells((update + 3), 2) = "PSS (kBs)"
worksheet.Cells((update + 3), 3) = "USS (kBs)"
worksheet.Cells((update + 3), 4) = "User %"
worksheet.Cells((update + 3), 5) = "Kernel %"
worksheet.Cells((update + 3), 6) = "Total %"
worksheet.Cells((update + 4), 1) = "Min:"
worksheet.Cells((update + 5), 1) = "Max:"
worksheet.Cells((update + 6), 1) = "Average:"
worksheet.Cells((update + 7), 1) = "Median:"
worksheet.Cells((update + 8), 1) = "Stan Dev:"
end sub
I thought for sure the above would work but it still seems that I am missing something, when I open the excel sheet nothing was printed. This would cut down the lines of code that I have in half easily, so I would love to find a solution for this
Thanks Guys
.......................................................
Edit (Sorry those comment boxes are terrible for writing anything)
.......................................................
alright I tried changing these lines of code:
If ComboBox2.SelectedIndex = 1 Then
If ComboBox3.SelectedIndex = 0 Then
ExcelUpdate(xlWorkSheet202, AT, CDbl(Pvalue), CDbl(uvalue), CDbl(UserRx.Match(line).Value), CDbl(KernelRx.Match(line).Value))
ElseIf ComboBox3.SelectedIndex = 1 Then
ExcelUpdate(xlWorkSheet203, GT, CDbl(Pvalue), CDbl(uvalue), CDbl(UserRx.Match(line).Value), CDbl(KernelRx.Match(line).Value))
ElseIf ComboBox3.SelectedIndex = 2 Then
ExcelUpdate(xlWorkSheet204, WT, CDbl(Pvalue), CDbl(uvalue), CDbl(UserRx.Match(line).Value), CDbl(KernelRx.Match(line).Value))
ElseIf ComboBox3.SelectedIndex = 3 Then
ExcelUpdate(xlWorkSheet205, OT, CDbl(Pvalue), CDbl(uvalue), CDbl(UserRx.Match(line).Value), CDbl(KernelRx.Match(line).Value))
End If
End If
Private Sub ExcelUpdate(ByVal Sheet As Object, ByVal update As Integer, ByVal pval As Double, ByVal uval As Double, ByVal user As Double, ByVal kernel As Double)
update = update + 1
Sheet.cells(update, 1) = timenow
Sheet.cells(update, 2) = pval
Sheet.cells(update, 3) = uval
Sheet.cells(update, 4) = user
Sheet.cells(update, 5) = kernel
Sheet.cells(update, 6) = cdbl(kernel + User)
end sub
But the excel sheets still do not update with the new information. Is there anything else im missing?
I would check/change a couple of things:
1) Change the ByRefs in the function to ByVal. You don't need to update the reference to the worksheet or modify the string, so ByRef is not need.
2) Determine the data type of the update parameter. You are mixing operation and types, which could result in an incorrect cells reference.
If the goal of the cell reference is:
worksheet.Cells(("A2"), 3)
then you should change your code to:
worksheet.Cells((update & "2"), 3)
If the goal of the cell reference is:
worksheet.Cells((12), 3)
then you should change the update parameter type:
update as integer